Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
Fix converter creation at export time
Browse files Browse the repository at this point in the history
  • Loading branch information
argenos committed Mar 28, 2021
1 parent 1f0c3f2 commit daf4029
Showing 1 changed file with 39 additions and 33 deletions.
72 changes: 39 additions & 33 deletions src/markdown-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@ const css = require('css');
var shajs = require('sha.js')
const Turndown = require('joplin-turndown').default

// Create a single Turndown provider which we'll use for all exporting. This
// instance will be generated during Zotero load and will be kept in memory for
// as long as the app is running.
const converter = new Turndown({
headingStyle: 'atx',
hr: '---',
bulletListMarker: Zotero.Prefs.get('extensions.mdnotes.html2md.default.bullet', true),
strongDelimiter: Zotero.Prefs.get('extensions.mdnotes.html2md.default.strong', true),
emDelimiter: Zotero.Prefs.get('extensions.mdnotes.html2md.default.em', true)
})

function hasStyle(node, property, value) {
// From https://github.com/laurent22/joplin-turndown/blob/master/src/commonmark-rules.js#L150
if (!node.nodeName =='SPAN') return false;
Expand All @@ -31,28 +20,44 @@ function hasStyle(node, property, value) {

}

converter.addRule('strikethrough', {
// filter: ['del', 's', 'strike'],
filter: function (node) {
return hasStyle(node, 'text-decoration', 'line-through') || node.nodeName === 'S' || node.nodeName === 'DEL' || node.nodeName === 'STRIKE';
},
replacement: function (content) {
let delimiter = Zotero.Prefs.get("extensions.mdnotes.html2md.default.strikethrough", true);
return delimiter + content + delimiter;
}
})

converter.addRule('underline', {
// filter: 'u',
filter: function (node) {
return hasStyle(node, 'text-decoration', 'underline') || node.nodeName === 'U';
},
replacement: function(content) {
let open = Zotero.Prefs.get(`extensions.mdnotes.html2md.rules.underline.open`, true);
let close = Zotero.Prefs.get(`extensions.mdnotes.html2md.rules.underline.close`, true);
return open + content + close;
}
})

function getConverter(){
// Create a single Turndown provider which we'll use for all exporting. This
// instance will be generated during Zotero load and will be kept in memory for
// as long as the app is running.
const converter = new Turndown({
headingStyle: 'atx',
hr: '---',
bulletListMarker: Zotero.Prefs.get('extensions.mdnotes.html2md.default.bullet', true),
strongDelimiter: Zotero.Prefs.get('extensions.mdnotes.html2md.default.strong', true),
emDelimiter: Zotero.Prefs.get('extensions.mdnotes.html2md.default.em', true)
})

converter.addRule('strikethrough', {
// filter: ['del', 's', 'strike'],
filter: function (node) {
return hasStyle(node, 'text-decoration', 'line-through') || node.nodeName === 'S' || node.nodeName === 'DEL' || node.nodeName === 'STRIKE';
},
replacement: function (content) {
let delimiter = Zotero.Prefs.get("extensions.mdnotes.html2md.default.strikethrough", true);
return delimiter + content + delimiter;
}
})

converter.addRule('underline', {
// filter: 'u',
filter: function (node) {
return hasStyle(node, 'text-decoration', 'underline') || node.nodeName === 'U';
},
replacement: function(content) {
let open = Zotero.Prefs.get(`extensions.mdnotes.html2md.rules.underline.open`, true);
let close = Zotero.Prefs.get(`extensions.mdnotes.html2md.rules.underline.close`, true);
return open + content + close;
}
})

return converter;
}

// Attach all utility functions to the Zotero.MarkdownUtils object. Any file
// that has access to the Zotero object can then use it.
Expand All @@ -65,6 +70,7 @@ Zotero.MarkdownUtils = new function () {
* @return {string} The Markdown result
*/
this.html2md = function (html) {
const converter = getConverter();
return converter.turndown(html)
}

Expand Down

0 comments on commit daf4029

Please sign in to comment.