From fbc0304797d144ec80820ae951cb9b323dd0fa9a Mon Sep 17 00:00:00 2001 From: telesoho Date: Sun, 5 Nov 2023 14:08:24 +0800 Subject: [PATCH] :new: add new turndownOptions option --- package-lock.json | 14 +++++++------- package.json | 21 ++++++++++++--------- src/paster.ts | 4 ++-- src/toMarkdown.ts | 38 ++++++-------------------------------- 4 files changed, 27 insertions(+), 50 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3458542..08bec7b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,7 @@ "arch": "^2.2.0", "moment": "^2.22.1", "shelljs": "^0.8.5", - "turndown": "^7.1.1", + "turndown": "^7.1.2", "xclip": "^1.0.3" }, "devDependencies": { @@ -3905,9 +3905,9 @@ } }, "node_modules/turndown": { - "version": "7.1.1", - "resolved": "https://registry.npmjs.org/turndown/-/turndown-7.1.1.tgz", - "integrity": "sha512-BEkXaWH7Wh7e9bd2QumhfAXk5g34+6QUmmWx+0q6ThaVOLuLUqsnkq35HQ5SBHSaxjSfSM7US5o4lhJNH7B9MA==", + "version": "7.1.2", + "resolved": "https://registry.npmjs.org/turndown/-/turndown-7.1.2.tgz", + "integrity": "sha512-ntI9R7fcUKjqBP6QU8rBK2Ehyt8LAzt3UBT9JR9tgo6GtuKvyUzpayWmeMKJw1DPdXzktvtIT8m2mVXz+bL/Qg==", "dependencies": { "domino": "^2.1.6" } @@ -7035,9 +7035,9 @@ } }, "turndown": { - "version": "7.1.1", - "resolved": "https://registry.npmjs.org/turndown/-/turndown-7.1.1.tgz", - "integrity": "sha512-BEkXaWH7Wh7e9bd2QumhfAXk5g34+6QUmmWx+0q6ThaVOLuLUqsnkq35HQ5SBHSaxjSfSM7US5o4lhJNH7B9MA==", + "version": "7.1.2", + "resolved": "https://registry.npmjs.org/turndown/-/turndown-7.1.2.tgz", + "integrity": "sha512-ntI9R7fcUKjqBP6QU8rBK2Ehyt8LAzt3UBT9JR9tgo6GtuKvyUzpayWmeMKJw1DPdXzktvtIT8m2mVXz+bL/Qg==", "requires": { "domino": "^2.1.6" } diff --git a/package.json b/package.json index 85de930..63a5833 100644 --- a/package.json +++ b/package.json @@ -97,15 +97,18 @@ "default": true, "description": "Enable/disable converting html to markdown." }, - "MarkdownPaste.headerStyle": { - "type": "string", + "MarkdownPaste.turndownOptions": { + "type": "object", "scope": "resource", - "enum": [ - "setext", - "atx" - ], - "default": "setext", - "description": "Use setext or atx style markdown headers." + "default": { + "headingStyle": "setext", + "bulletListMarker": "-", + "strongDelimiter": "**", + "emDelimiter": "*", + "preformattedCode": "true", + "hr": "\n\n* * * *\n\n" + }, + "description": "Use turndown options: https://github.com/mixmark-io/turndown#options" }, "MarkdownPaste.enableRulesForHtml": { "type": "boolean", @@ -277,7 +280,7 @@ "arch": "^2.2.0", "moment": "^2.22.1", "shelljs": "^0.8.5", - "turndown": "^7.1.1", + "turndown": "^7.1.2", "xclip": "^1.0.3" }, "devDependencies": { diff --git a/src/paster.ts b/src/paster.ts index 12b9235..370987d 100644 --- a/src/paster.ts +++ b/src/paster.ts @@ -46,7 +46,7 @@ class Paster { let enableHtmlConverter = Paster.getConfig().enableHtmlConverter; let enableRulesForHtml = Paster.getConfig().enableRulesForHtml; - let headerStyle = Paster.getConfig().headerStyle; + let turndownOptions = Paster.getConfig().turndownOptions; Logger.log("Clipboard Type:", ctx_type); switch (ctx_type) { @@ -54,7 +54,7 @@ class Paster { if (enableHtmlConverter) { const html = await cb.getTextHtml(); Logger.log(html); - const markdown = toMarkdown(html, headerStyle); + const markdown = toMarkdown(html, turndownOptions); if (enableRulesForHtml) { let newMarkdown = Paster.parse(markdown); Paster.writeToEditor(newMarkdown); diff --git a/src/toMarkdown.ts b/src/toMarkdown.ts index cfd1d4d..ca9ca8f 100644 --- a/src/toMarkdown.ts +++ b/src/toMarkdown.ts @@ -28,7 +28,7 @@ function cell(content, node) { return prefix + content + " " + suffix; } -function toMarkdown(content, headingStyle) { +function toMarkdown(content, options) { // http://pandoc.org/README.html#pandocs-markdown const pandoc = [ // { @@ -67,18 +67,10 @@ function toMarkdown(content, headingStyle) { return "\n"; }, }, - - { - filter: "hr", - replacement: function () { - return "\n\n* * * * *\n\n"; - }, - }, - { filter: ["em", "i", "cite", "var"], - replacement: function (content) { - return "*" + content + "*"; + replacement: function (content, node, options) { + return options.emDelimiter + content + options.emDelimiter; }, }, @@ -94,7 +86,7 @@ function toMarkdown(content, headingStyle) { return isCodeElem && !isCodeBlock; }, - replacement: function (content) { + replacement: function (content, node, options) { return "`" + content + "`"; }, }, @@ -103,7 +95,7 @@ function toMarkdown(content, headingStyle) { filter: function (node) { return node.nodeName === "A" && node.getAttribute("href"); }, - replacement: function (content, node) { + replacement: function (content, node, options) { const url = node.getAttribute("href"); const titlePart = node.title ? ' "' + node.title + '"' : ""; if (content === url) { @@ -118,24 +110,6 @@ function toMarkdown(content, headingStyle) { }, }, - { - filter: "li", - replacement: function (content, node) { - content = content.replace(/^\s+/, "").replace(/\n/gm, "\n "); - let prefix = "- "; - const parent = node.parentNode; - - if (/ol/i.test(parent.nodeName)) { - const index = Array.prototype.indexOf.call(parent.children, node) + 1; - prefix = index + ". "; - while (prefix.length < 4) { - prefix += " "; - } - } - - return prefix + content; - }, - }, { filter: ["font", "span"], replacement: function (content) { @@ -254,7 +228,7 @@ function toMarkdown(content, headingStyle) { }; var TurndownService = require("turndown"); - var turndownService = new TurndownService({ headingStyle }); + var turndownService = new TurndownService(options); Object.entries(pandoc).forEach(([key, value]) => { turndownService.addRule(key, value); });