Skip to content

Commit

Permalink
🆕 add new turndownOptions option
Browse files Browse the repository at this point in the history
  • Loading branch information
telesoho committed Nov 5, 2023
1 parent fac37c1 commit fbc0304
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 50 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 12 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions src/paster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ 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) {
case xclip.ClipboardType.Html:
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);
Expand Down
38 changes: 6 additions & 32 deletions src/toMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
// {
Expand Down Expand Up @@ -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;
},
},

Expand All @@ -94,7 +86,7 @@ function toMarkdown(content, headingStyle) {

return isCodeElem && !isCodeBlock;
},
replacement: function (content) {
replacement: function (content, node, options) {
return "`" + content + "`";
},
},
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit fbc0304

Please sign in to comment.