Turndown.js 26 KB

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