turndown.browser.cjs.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. 'use strict';
  2. function extend (destination) {
  3. for (var i = 1; i < arguments.length; i++) {
  4. var source = arguments[i];
  5. for (var key in source) {
  6. if (source.hasOwnProperty(key)) destination[key] = source[key];
  7. }
  8. }
  9. return destination
  10. }
  11. function repeat (character, count) {
  12. return Array(count + 1).join(character)
  13. }
  14. var blockElements = [
  15. 'address', 'article', 'aside', 'audio', 'blockquote', 'body', 'canvas',
  16. 'center', 'dd', 'dir', 'div', 'dl', 'dt', 'fieldset', 'figcaption',
  17. 'figure', 'footer', 'form', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
  18. 'header', 'hgroup', 'hr', 'html', 'isindex', 'li', 'main', 'menu', 'nav',
  19. 'noframes', 'noscript', 'ol', 'output', 'p', 'pre', 'section', 'table',
  20. 'tbody', 'td', 'tfoot', 'th', 'thead', 'tr', 'ul'
  21. ];
  22. function isBlock (node) {
  23. return blockElements.indexOf(node.nodeName.toLowerCase()) !== -1
  24. }
  25. var voidElements = [
  26. 'area', 'base', 'br', 'col', 'command', 'embed', 'hr', 'img', 'input',
  27. 'keygen', 'link', 'meta', 'param', 'source', 'track', 'wbr'
  28. ];
  29. function isVoid (node) {
  30. return voidElements.indexOf(node.nodeName.toLowerCase()) !== -1
  31. }
  32. var voidSelector = voidElements.join();
  33. function hasVoid (node) {
  34. return node.querySelector && node.querySelector(voidSelector)
  35. }
  36. var rules = {};
  37. rules.paragraph = {
  38. filter: 'p',
  39. replacement: function (content) {
  40. return '\n\n' + content + '\n\n'
  41. }
  42. };
  43. rules.lineBreak = {
  44. filter: 'br',
  45. replacement: function (content, node, options) {
  46. return options.br + '\n'
  47. }
  48. };
  49. rules.heading = {
  50. filter: ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'],
  51. replacement: function (content, node, options) {
  52. var hLevel = Number(node.nodeName.charAt(1));
  53. if (options.headingStyle === 'setext' && hLevel < 3) {
  54. var underline = repeat((hLevel === 1 ? '=' : '-'), content.length);
  55. return (
  56. '\n\n' + content + '\n' + underline + '\n\n'
  57. )
  58. } else {
  59. return '\n\n' + repeat('#', hLevel) + ' ' + content + '\n\n'
  60. }
  61. }
  62. };
  63. rules.blockquote = {
  64. filter: 'blockquote',
  65. replacement: function (content) {
  66. content = content.replace(/^\n+|\n+$/g, '');
  67. content = content.replace(/^/gm, '> ');
  68. return '\n\n' + content + '\n\n'
  69. }
  70. };
  71. rules.list = {
  72. filter: ['ul', 'ol'],
  73. replacement: function (content, node) {
  74. var parent = node.parentNode;
  75. if (parent.nodeName === 'LI' && parent.lastElementChild === node) {
  76. return '\n' + content
  77. } else {
  78. return '\n\n' + content + '\n\n'
  79. }
  80. }
  81. };
  82. rules.listItem = {
  83. filter: 'li',
  84. replacement: function (content, node, options) {
  85. content = content
  86. .replace(/^\n+/, '') // remove leading newlines
  87. .replace(/\n+$/, '\n') // replace trailing newlines with just a single one
  88. .replace(/\n/gm, '\n '); // indent
  89. var prefix = options.bulletListMarker + ' ';
  90. var parent = node.parentNode;
  91. if (parent.nodeName === 'OL') {
  92. var start = parent.getAttribute('start');
  93. var index = Array.prototype.indexOf.call(parent.children, node);
  94. prefix = (start ? Number(start) + index : index + 1) + '. ';
  95. }
  96. return (
  97. prefix + content + (node.nextSibling && !/\n$/.test(content) ? '\n' : '')
  98. )
  99. }
  100. };
  101. rules.indentedCodeBlock = {
  102. filter: function (node, options) {
  103. return (
  104. options.codeBlockStyle === 'indented' &&
  105. node.nodeName === 'PRE' &&
  106. node.firstChild &&
  107. node.firstChild.nodeName === 'CODE'
  108. )
  109. },
  110. replacement: function (content, node, options) {
  111. return (
  112. '\n\n ' +
  113. node.firstChild.textContent.replace(/\n/g, '\n ') +
  114. '\n\n'
  115. )
  116. }
  117. };
  118. rules.fencedCodeBlock = {
  119. filter: function (node, options) {
  120. return (
  121. options.codeBlockStyle === 'fenced' &&
  122. node.nodeName === 'PRE' &&
  123. node.firstChild &&
  124. node.firstChild.nodeName === 'CODE'
  125. )
  126. },
  127. replacement: function (content, node, options) {
  128. var className = node.firstChild.className || '';
  129. var language = (className.match(/language-(\S+)/) || [null, ''])[1];
  130. var code = node.firstChild.textContent;
  131. var fenceChar = options.fence.charAt(0);
  132. var fenceSize = 3;
  133. var fenceInCodeRegex = new RegExp('^' + fenceChar + '{3,}', 'gm');
  134. var match;
  135. while ((match = fenceInCodeRegex.exec(code))) {
  136. if (match[0].length >= fenceSize) {
  137. fenceSize = match[0].length + 1;
  138. }
  139. }
  140. var fence = repeat(fenceChar, fenceSize);
  141. return (
  142. '\n\n' + fence + language + '\n' +
  143. code.replace(/\n$/, '') +
  144. '\n' + fence + '\n\n'
  145. )
  146. }
  147. };
  148. rules.horizontalRule = {
  149. filter: 'hr',
  150. replacement: function (content, node, options) {
  151. return '\n\n' + options.hr + '\n\n'
  152. }
  153. };
  154. rules.inlineLink = {
  155. filter: function (node, options) {
  156. return (
  157. options.linkStyle === 'inlined' &&
  158. node.nodeName === 'A' &&
  159. node.getAttribute('href')
  160. )
  161. },
  162. replacement: function (content, node) {
  163. var href = node.getAttribute('href');
  164. var title = node.title ? ' "' + node.title + '"' : '';
  165. return '[' + content + '](' + href + title + ')'
  166. }
  167. };
  168. rules.referenceLink = {
  169. filter: function (node, options) {
  170. return (
  171. options.linkStyle === 'referenced' &&
  172. node.nodeName === 'A' &&
  173. node.getAttribute('href')
  174. )
  175. },
  176. replacement: function (content, node, options) {
  177. var href = node.getAttribute('href');
  178. var title = node.title ? ' "' + node.title + '"' : '';
  179. var replacement;
  180. var reference;
  181. switch (options.linkReferenceStyle) {
  182. case 'collapsed':
  183. replacement = '[' + content + '][]';
  184. reference = '[' + content + ']: ' + href + title;
  185. break
  186. case 'shortcut':
  187. replacement = '[' + content + ']';
  188. reference = '[' + content + ']: ' + href + title;
  189. break
  190. default:
  191. var id = this.references.length + 1;
  192. replacement = '[' + content + '][' + id + ']';
  193. reference = '[' + id + ']: ' + href + title;
  194. }
  195. this.references.push(reference);
  196. return replacement
  197. },
  198. references: [],
  199. append: function (options) {
  200. var references = '';
  201. if (this.references.length) {
  202. references = '\n\n' + this.references.join('\n') + '\n\n';
  203. this.references = []; // Reset references
  204. }
  205. return references
  206. }
  207. };
  208. rules.emphasis = {
  209. filter: ['em', 'i'],
  210. replacement: function (content, node, options) {
  211. if (!content.trim()) return ''
  212. return options.emDelimiter + content + options.emDelimiter
  213. }
  214. };
  215. rules.strong = {
  216. filter: ['strong', 'b'],
  217. replacement: function (content, node, options) {
  218. if (!content.trim()) return ''
  219. return options.strongDelimiter + content + options.strongDelimiter
  220. }
  221. };
  222. rules.code = {
  223. filter: function (node) {
  224. var hasSiblings = node.previousSibling || node.nextSibling;
  225. var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings;
  226. return node.nodeName === 'CODE' && !isCodeBlock
  227. },
  228. replacement: function (content) {
  229. if (!content.trim()) return ''
  230. var delimiter = '`';
  231. var leadingSpace = '';
  232. var trailingSpace = '';
  233. var matches = content.match(/`+/gm);
  234. if (matches) {
  235. if (/^`/.test(content)) leadingSpace = ' ';
  236. if (/`$/.test(content)) trailingSpace = ' ';
  237. while (matches.indexOf(delimiter) !== -1) delimiter = delimiter + '`';
  238. }
  239. return delimiter + leadingSpace + content + trailingSpace + delimiter
  240. }
  241. };
  242. rules.image = {
  243. filter: 'img',
  244. replacement: function (content, node) {
  245. var alt = node.alt || '';
  246. var src = node.getAttribute('src') || '';
  247. var title = node.title || '';
  248. var titlePart = title ? ' "' + title + '"' : '';
  249. return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : ''
  250. }
  251. };
  252. /**
  253. * Manages a collection of rules used to convert HTML to Markdown
  254. */
  255. function Rules (options) {
  256. this.options = options;
  257. this._keep = [];
  258. this._remove = [];
  259. this.blankRule = {
  260. replacement: options.blankReplacement
  261. };
  262. this.keepReplacement = options.keepReplacement;
  263. this.defaultRule = {
  264. replacement: options.defaultReplacement
  265. };
  266. this.array = [];
  267. for (var key in options.rules) this.array.push(options.rules[key]);
  268. }
  269. Rules.prototype = {
  270. add: function (key, rule) {
  271. this.array.unshift(rule);
  272. },
  273. keep: function (filter) {
  274. this._keep.unshift({
  275. filter: filter,
  276. replacement: this.keepReplacement
  277. });
  278. },
  279. remove: function (filter) {
  280. this._remove.unshift({
  281. filter: filter,
  282. replacement: function () {
  283. return ''
  284. }
  285. });
  286. },
  287. forNode: function (node) {
  288. if (node.isBlank) return this.blankRule
  289. var rule;
  290. if ((rule = findRule(this.array, node, this.options))) return rule
  291. if ((rule = findRule(this._keep, node, this.options))) return rule
  292. if ((rule = findRule(this._remove, node, this.options))) return rule
  293. return this.defaultRule
  294. },
  295. forEach: function (fn) {
  296. for (var i = 0; i < this.array.length; i++) fn(this.array[i], i);
  297. }
  298. };
  299. function findRule (rules, node, options) {
  300. for (var i = 0; i < rules.length; i++) {
  301. var rule = rules[i];
  302. if (filterValue(rule, node, options)) return rule
  303. }
  304. return void 0
  305. }
  306. function filterValue (rule, node, options) {
  307. var filter = rule.filter;
  308. if (typeof filter === 'string') {
  309. if (filter === node.nodeName.toLowerCase()) return true
  310. } else if (Array.isArray(filter)) {
  311. if (filter.indexOf(node.nodeName.toLowerCase()) > -1) return true
  312. } else if (typeof filter === 'function') {
  313. if (filter.call(rule, node, options)) return true
  314. } else {
  315. throw new TypeError('`filter` needs to be a string, array, or function')
  316. }
  317. }
  318. /**
  319. * The collapseWhitespace function is adapted from collapse-whitespace
  320. * by Luc Thevenard.
  321. *
  322. * The MIT License (MIT)
  323. *
  324. * Copyright (c) 2014 Luc Thevenard <lucthevenard@gmail.com>
  325. *
  326. * Permission is hereby granted, free of charge, to any person obtaining a copy
  327. * of this software and associated documentation files (the "Software"), to deal
  328. * in the Software without restriction, including without limitation the rights
  329. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  330. * copies of the Software, and to permit persons to whom the Software is
  331. * furnished to do so, subject to the following conditions:
  332. *
  333. * The above copyright notice and this permission notice shall be included in
  334. * all copies or substantial portions of the Software.
  335. *
  336. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  337. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  338. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  339. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  340. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  341. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  342. * THE SOFTWARE.
  343. */
  344. /**
  345. * collapseWhitespace(options) removes extraneous whitespace from an the given element.
  346. *
  347. * @param {Object} options
  348. */
  349. function collapseWhitespace (options) {
  350. var element = options.element;
  351. var isBlock = options.isBlock;
  352. var isVoid = options.isVoid;
  353. var isPre = options.isPre || function (node) {
  354. return node.nodeName === 'PRE'
  355. };
  356. if (!element.firstChild || isPre(element)) return
  357. var prevText = null;
  358. var prevVoid = false;
  359. var prev = null;
  360. var node = next(prev, element, isPre);
  361. while (node !== element) {
  362. if (node.nodeType === 3 || node.nodeType === 4) { // Node.TEXT_NODE or Node.CDATA_SECTION_NODE
  363. var text = node.data.replace(/[ \r\n\t]+/g, ' ');
  364. if ((!prevText || / $/.test(prevText.data)) &&
  365. !prevVoid && text[0] === ' ') {
  366. text = text.substr(1);
  367. }
  368. // `text` might be empty at this point.
  369. if (!text) {
  370. node = remove(node);
  371. continue
  372. }
  373. node.data = text;
  374. prevText = node;
  375. } else if (node.nodeType === 1) { // Node.ELEMENT_NODE
  376. if (isBlock(node) || node.nodeName === 'BR') {
  377. if (prevText) {
  378. prevText.data = prevText.data.replace(/ $/, '');
  379. }
  380. prevText = null;
  381. prevVoid = false;
  382. } else if (isVoid(node)) {
  383. // Avoid trimming space around non-block, non-BR void elements.
  384. prevText = null;
  385. prevVoid = true;
  386. }
  387. } else {
  388. node = remove(node);
  389. continue
  390. }
  391. var nextNode = next(prev, node, isPre);
  392. prev = node;
  393. node = nextNode;
  394. }
  395. if (prevText) {
  396. prevText.data = prevText.data.replace(/ $/, '');
  397. if (!prevText.data) {
  398. remove(prevText);
  399. }
  400. }
  401. }
  402. /**
  403. * remove(node) removes the given node from the DOM and returns the
  404. * next node in the sequence.
  405. *
  406. * @param {Node} node
  407. * @return {Node} node
  408. */
  409. function remove (node) {
  410. var next = node.nextSibling || node.parentNode;
  411. node.parentNode.removeChild(node);
  412. return next
  413. }
  414. /**
  415. * next(prev, current, isPre) returns the next node in the sequence, given the
  416. * current and previous nodes.
  417. *
  418. * @param {Node} prev
  419. * @param {Node} current
  420. * @param {Function} isPre
  421. * @return {Node}
  422. */
  423. function next (prev, current, isPre) {
  424. if ((prev && prev.parentNode === current) || isPre(current)) {
  425. return current.nextSibling || current.parentNode
  426. }
  427. return current.firstChild || current.nextSibling || current.parentNode
  428. }
  429. /*
  430. * Set up window for Node.js
  431. */
  432. var root = (typeof window !== 'undefined' ? window : {});
  433. /*
  434. * Parsing HTML strings
  435. */
  436. function canParseHTMLNatively () {
  437. var Parser = root.DOMParser;
  438. var canParse = false;
  439. // Adapted from https://gist.github.com/1129031
  440. // Firefox/Opera/IE throw errors on unsupported types
  441. try {
  442. // WebKit returns null on unsupported types
  443. if (new Parser().parseFromString('', 'text/html')) {
  444. canParse = true;
  445. }
  446. } catch (e) {}
  447. return canParse
  448. }
  449. function createHTMLParser () {
  450. var Parser = function () {};
  451. {
  452. if (shouldUseActiveX()) {
  453. Parser.prototype.parseFromString = function (string) {
  454. var doc = new window.ActiveXObject('htmlfile');
  455. doc.designMode = 'on'; // disable on-page scripts
  456. doc.open();
  457. doc.write(string);
  458. doc.close();
  459. return doc
  460. };
  461. } else {
  462. Parser.prototype.parseFromString = function (string) {
  463. var doc = document.implementation.createHTMLDocument('');
  464. doc.open();
  465. doc.write(string);
  466. doc.close();
  467. return doc
  468. };
  469. }
  470. }
  471. return Parser
  472. }
  473. function shouldUseActiveX () {
  474. var useActiveX = false;
  475. try {
  476. document.implementation.createHTMLDocument('').open();
  477. } catch (e) {
  478. if (window.ActiveXObject) useActiveX = true;
  479. }
  480. return useActiveX
  481. }
  482. var HTMLParser = canParseHTMLNatively() ? root.DOMParser : createHTMLParser();
  483. function RootNode (input) {
  484. var root;
  485. if (typeof input === 'string') {
  486. var doc = htmlParser().parseFromString(
  487. // DOM parsers arrange elements in the <head> and <body>.
  488. // Wrapping in a custom element ensures elements are reliably arranged in
  489. // a single element.
  490. '<x-turndown id="turndown-root">' + input + '</x-turndown>',
  491. 'text/html'
  492. );
  493. root = doc.getElementById('turndown-root');
  494. } else {
  495. root = input.cloneNode(true);
  496. }
  497. collapseWhitespace({
  498. element: root,
  499. isBlock: isBlock,
  500. isVoid: isVoid
  501. });
  502. return root
  503. }
  504. var _htmlParser;
  505. function htmlParser () {
  506. _htmlParser = _htmlParser || new HTMLParser();
  507. return _htmlParser
  508. }
  509. function Node (node) {
  510. node.isBlock = isBlock(node);
  511. node.isCode = node.nodeName.toLowerCase() === 'code' || node.parentNode.isCode;
  512. node.isBlank = isBlank(node);
  513. node.flankingWhitespace = flankingWhitespace(node);
  514. return node
  515. }
  516. function isBlank (node) {
  517. return (
  518. ['A', 'TH', 'TD', 'IFRAME', 'SCRIPT', 'AUDIO', 'VIDEO'].indexOf(node.nodeName) === -1 &&
  519. /^\s*$/i.test(node.textContent) &&
  520. !isVoid(node) &&
  521. !hasVoid(node)
  522. )
  523. }
  524. function flankingWhitespace (node) {
  525. var leading = '';
  526. var trailing = '';
  527. if (!node.isBlock) {
  528. var hasLeading = /^\s/.test(node.textContent);
  529. var hasTrailing = /\s$/.test(node.textContent);
  530. var blankWithSpaces = node.isBlank && hasLeading && hasTrailing;
  531. if (hasLeading && !isFlankedByWhitespace('left', node)) {
  532. leading = ' ';
  533. }
  534. if (!blankWithSpaces && hasTrailing && !isFlankedByWhitespace('right', node)) {
  535. trailing = ' ';
  536. }
  537. }
  538. return { leading: leading, trailing: trailing }
  539. }
  540. function isFlankedByWhitespace (side, node) {
  541. var sibling;
  542. var regExp;
  543. var isFlanked;
  544. if (side === 'left') {
  545. sibling = node.previousSibling;
  546. regExp = / $/;
  547. } else {
  548. sibling = node.nextSibling;
  549. regExp = /^ /;
  550. }
  551. if (sibling) {
  552. if (sibling.nodeType === 3) {
  553. isFlanked = regExp.test(sibling.nodeValue);
  554. } else if (sibling.nodeType === 1 && !isBlock(sibling)) {
  555. isFlanked = regExp.test(sibling.textContent);
  556. }
  557. }
  558. return isFlanked
  559. }
  560. var reduce = Array.prototype.reduce;
  561. var leadingNewLinesRegExp = /^\n*/;
  562. var trailingNewLinesRegExp = /\n*$/;
  563. var escapes = [
  564. [/\\/g, '\\\\'],
  565. [/\*/g, '\\*'],
  566. [/^-/g, '\\-'],
  567. [/^\+ /g, '\\+ '],
  568. [/^(=+)/g, '\\$1'],
  569. [/^(#{1,6}) /g, '\\$1 '],
  570. [/`/g, '\\`'],
  571. [/^~~~/g, '\\~~~'],
  572. [/\[/g, '\\['],
  573. [/\]/g, '\\]'],
  574. [/^>/g, '\\>'],
  575. [/_/g, '\\_'],
  576. [/^(\d+)\. /g, '$1\\. ']
  577. ];
  578. function TurndownService (options) {
  579. if (!(this instanceof TurndownService)) return new TurndownService(options)
  580. var defaults = {
  581. rules: rules,
  582. headingStyle: 'setext',
  583. hr: '* * *',
  584. bulletListMarker: '*',
  585. codeBlockStyle: 'indented',
  586. fence: '```',
  587. emDelimiter: '_',
  588. strongDelimiter: '**',
  589. linkStyle: 'inlined',
  590. linkReferenceStyle: 'full',
  591. br: ' ',
  592. blankReplacement: function (content, node) {
  593. return node.isBlock ? '\n\n' : ''
  594. },
  595. keepReplacement: function (content, node) {
  596. return node.isBlock ? '\n\n' + node.outerHTML + '\n\n' : node.outerHTML
  597. },
  598. defaultReplacement: function (content, node) {
  599. return node.isBlock ? '\n\n' + content + '\n\n' : content
  600. }
  601. };
  602. this.options = extend({}, defaults, options);
  603. this.rules = new Rules(this.options);
  604. }
  605. TurndownService.prototype = {
  606. /**
  607. * The entry point for converting a string or DOM node to Markdown
  608. * @public
  609. * @param {String|HTMLElement} input The string or DOM node to convert
  610. * @returns A Markdown representation of the input
  611. * @type String
  612. */
  613. turndown: function (input) {
  614. if (!canConvert(input)) {
  615. throw new TypeError(
  616. input + ' is not a string, or an element/document/fragment node.'
  617. )
  618. }
  619. if (input === '') return ''
  620. var output = process.call(this, new RootNode(input));
  621. return postProcess.call(this, output)
  622. },
  623. /**
  624. * Add one or more plugins
  625. * @public
  626. * @param {Function|Array} plugin The plugin or array of plugins to add
  627. * @returns The Turndown instance for chaining
  628. * @type Object
  629. */
  630. use: function (plugin) {
  631. if (Array.isArray(plugin)) {
  632. for (var i = 0; i < plugin.length; i++) this.use(plugin[i]);
  633. } else if (typeof plugin === 'function') {
  634. plugin(this);
  635. } else {
  636. throw new TypeError('plugin must be a Function or an Array of Functions')
  637. }
  638. return this
  639. },
  640. /**
  641. * Adds a rule
  642. * @public
  643. * @param {String} key The unique key of the rule
  644. * @param {Object} rule The rule
  645. * @returns The Turndown instance for chaining
  646. * @type Object
  647. */
  648. addRule: function (key, rule) {
  649. this.rules.add(key, rule);
  650. return this
  651. },
  652. /**
  653. * Keep a node (as HTML) that matches the filter
  654. * @public
  655. * @param {String|Array|Function} filter The unique key of the rule
  656. * @returns The Turndown instance for chaining
  657. * @type Object
  658. */
  659. keep: function (filter) {
  660. this.rules.keep(filter);
  661. return this
  662. },
  663. /**
  664. * Remove a node that matches the filter
  665. * @public
  666. * @param {String|Array|Function} filter The unique key of the rule
  667. * @returns The Turndown instance for chaining
  668. * @type Object
  669. */
  670. remove: function (filter) {
  671. this.rules.remove(filter);
  672. return this
  673. },
  674. /**
  675. * Escapes Markdown syntax
  676. * @public
  677. * @param {String} string The string to escape
  678. * @returns A string with Markdown syntax escaped
  679. * @type String
  680. */
  681. escape: function (string) {
  682. return escapes.reduce(function (accumulator, escape) {
  683. return accumulator.replace(escape[0], escape[1])
  684. }, string)
  685. }
  686. };
  687. /**
  688. * Reduces a DOM node down to its Markdown string equivalent
  689. * @private
  690. * @param {HTMLElement} parentNode The node to convert
  691. * @returns A Markdown representation of the node
  692. * @type String
  693. */
  694. function process (parentNode) {
  695. var self = this;
  696. return reduce.call(parentNode.childNodes, function (output, node) {
  697. node = new Node(node);
  698. var replacement = '';
  699. if (node.nodeType === 3) {
  700. replacement = node.isCode ? node.nodeValue : self.escape(node.nodeValue);
  701. } else if (node.nodeType === 1) {
  702. replacement = replacementForNode.call(self, node);
  703. }
  704. return join(output, replacement)
  705. }, '')
  706. }
  707. /**
  708. * Appends strings as each rule requires and trims the output
  709. * @private
  710. * @param {String} output The conversion output
  711. * @returns A trimmed version of the ouput
  712. * @type String
  713. */
  714. function postProcess (output) {
  715. var self = this;
  716. this.rules.forEach(function (rule) {
  717. if (typeof rule.append === 'function') {
  718. output = join(output, rule.append(self.options));
  719. }
  720. });
  721. return output.replace(/^[\t\r\n]+/, '').replace(/[\t\r\n\s]+$/, '')
  722. }
  723. /**
  724. * Converts an element node to its Markdown equivalent
  725. * @private
  726. * @param {HTMLElement} node The node to convert
  727. * @returns A Markdown representation of the node
  728. * @type String
  729. */
  730. function replacementForNode (node) {
  731. var rule = this.rules.forNode(node);
  732. var content = process.call(this, node);
  733. var whitespace = node.flankingWhitespace;
  734. if (whitespace.leading || whitespace.trailing) content = content.trim();
  735. return (
  736. whitespace.leading +
  737. rule.replacement(content, node, this.options) +
  738. whitespace.trailing
  739. )
  740. }
  741. /**
  742. * Determines the new lines between the current output and the replacement
  743. * @private
  744. * @param {String} output The current conversion output
  745. * @param {String} replacement The string to append to the output
  746. * @returns The whitespace to separate the current output and the replacement
  747. * @type String
  748. */
  749. function separatingNewlines (output, replacement) {
  750. var newlines = [
  751. output.match(trailingNewLinesRegExp)[0],
  752. replacement.match(leadingNewLinesRegExp)[0]
  753. ].sort();
  754. var maxNewlines = newlines[newlines.length - 1];
  755. return maxNewlines.length < 2 ? maxNewlines : '\n\n'
  756. }
  757. function join (string1, string2) {
  758. var separator = separatingNewlines(string1, string2);
  759. // Remove trailing/leading newlines and replace with separator
  760. string1 = string1.replace(trailingNewLinesRegExp, '');
  761. string2 = string2.replace(leadingNewLinesRegExp, '');
  762. return string1 + separator + string2
  763. }
  764. /**
  765. * Determines whether an input can be converted
  766. * @private
  767. * @param {String|HTMLElement} input Describe this parameter
  768. * @returns Describe what it returns
  769. * @type String|Object|Array|Boolean|Number
  770. */
  771. function canConvert (input) {
  772. return (
  773. input != null && (
  774. typeof input === 'string' ||
  775. (input.nodeType && (
  776. input.nodeType === 1 || input.nodeType === 9 || input.nodeType === 11
  777. ))
  778. )
  779. )
  780. }
  781. module.exports = TurndownService;