Debug Turndown ES2020 #1604

This commit is contained in:
the-djmaze 2024-09-14 13:37:31 +02:00
parent 0db6c6ab5f
commit d3b0d6ca57

View file

@ -211,7 +211,7 @@ const TurndownService = (() => {
) )
, ,
replacement: function (content, node, options) { replacement(content, node, options) {
var href = node.getAttribute('href'); var href = node.getAttribute('href');
var title = cleanAttribute(node.getAttribute('title')); var title = cleanAttribute(node.getAttribute('title'));
if (title) title = ' "' + title + '"'; if (title) title = ' "' + title + '"';
@ -239,7 +239,7 @@ const TurndownService = (() => {
references: [], references: [],
append: function () { append() {
var references = ''; var references = '';
if (this.references.length) { if (this.references.length) {
references = '\n\n' + this.references.join('\n') + '\n\n'; references = '\n\n' + this.references.join('\n') + '\n\n';
@ -298,6 +298,11 @@ const TurndownService = (() => {
var titlePart = title ? ' "' + title + '"' : ''; var titlePart = title ? ' "' + title + '"' : '';
return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : '' return src ? '![' + alt + ']' + '(' + src + titlePart + ')' : ''
} }
},
style: {
filter: 'style',
replacement: () => ''
} }
}, },
@ -640,8 +645,6 @@ const TurndownService = (() => {
class TurndownService class TurndownService
{ {
constructor(options) { constructor(options) {
if (!(this instanceof TurndownService)) return new TurndownService(options)
this.options = Object.assign({ this.options = Object.assign({
rules: rules, rules: rules,
headingStyle: 'setext', headingStyle: 'setext',
@ -679,8 +682,8 @@ const TurndownService = (() => {
if (input === '') return '' if (input === '') return ''
var output = process.call(this, new RootNode(input, this.options)); var output = this.process(RootNode(input, this.options));
return postProcess.call(this, output) return this.postProcess(output)
} }
/** /**
@ -753,7 +756,6 @@ const TurndownService = (() => {
escape(string) { escape(string) {
return escapes.reduce((accumulator, escape) => accumulator.replace(escape[0], escape[1]), string) return escapes.reduce((accumulator, escape) => accumulator.replace(escape[0], escape[1]), string)
} }
}
/** /**
* Reduces a DOM node down to its Markdown string equivalent * Reduces a DOM node down to its Markdown string equivalent
@ -763,21 +765,21 @@ const TurndownService = (() => {
* @type String * @type String
*/ */
const process = (parentNode) => { process(parentNode) {
var self = this; var self = this;
return reduce.call(parentNode.childNodes, (output, node) => { return reduce.call(parentNode.childNodes, (output, node) => {
node = new Node(node, self.options); node = Node(node, self.options);
var replacement = ''; var replacement = '';
if (node.nodeType === 3) { if (node.nodeType === 3) {
replacement = node.isCode ? node.nodeValue : self.escape(node.nodeValue); replacement = node.isCode ? node.nodeValue : self.escape(node.nodeValue);
} else if (node.nodeType === 1) { } else if (node.nodeType === 1) {
replacement = replacementForNode.call(self, node); replacement = self.replacementForNode(node);
} }
return join(output, replacement) return join(output, replacement)
}, '') }, '')
}, }
/** /**
* Appends strings as each rule requires and trims the output * Appends strings as each rule requires and trims the output
@ -787,7 +789,7 @@ const TurndownService = (() => {
* @type String * @type String
*/ */
postProcess = (output) => { postProcess(output) {
var self = this; var self = this;
this.rules.forEach((rule) => { this.rules.forEach((rule) => {
if (typeof rule.append === 'function') { if (typeof rule.append === 'function') {
@ -796,7 +798,7 @@ const TurndownService = (() => {
}); });
return output.replace(/^[\t\r\n]+/, '').replace(/[\t\r\n\s]+$/, '') return output.replace(/^[\t\r\n]+/, '').replace(/[\t\r\n\s]+$/, '')
}, }
/** /**
* Converts an element node to its Markdown equivalent * Converts an element node to its Markdown equivalent
@ -806,9 +808,9 @@ const TurndownService = (() => {
* @type String * @type String
*/ */
replacementForNode = (node) => { replacementForNode(node) {
var rule = this.rules.forNode(node); var rule = this.rules.forNode(node);
var content = process.call(this, node); var content = this.process(node);
var whitespace = node.flankingWhitespace; var whitespace = node.flankingWhitespace;
if (whitespace.leading || whitespace.trailing) content = content.trim(); if (whitespace.leading || whitespace.trailing) content = content.trim();
return ( return (
@ -816,7 +818,8 @@ const TurndownService = (() => {
rule.replacement(content, node, this.options) + rule.replacement(content, node, this.options) +
whitespace.trailing whitespace.trailing
) )
}, }
}
/** /**
* Joins replacement to the current output with appropriate number of new lines * Joins replacement to the current output with appropriate number of new lines
@ -827,7 +830,7 @@ const TurndownService = (() => {
* @type String * @type String
*/ */
join = (output, replacement) => { const join = (output, replacement) => {
var s1 = trimTrailingNewlines(output); var s1 = trimTrailingNewlines(output);
var s2 = trimLeadingNewlines(replacement); var s2 = trimLeadingNewlines(replacement);
var nls = Math.max(output.length - s1.length, replacement.length - s2.length); var nls = Math.max(output.length - s1.length, replacement.length - s2.length);