Skip to content

Commit

Permalink
- 加入捕获code block
Browse files Browse the repository at this point in the history
  • Loading branch information
xiliuya committed Feb 17, 2023
1 parent 3a787b4 commit 042ba2c
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 71 deletions.
4 changes: 2 additions & 2 deletions capture.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@

this.selection_html = tempDiv.innerHTML;

var turndownService = new TurndownService();
var turndownService = new TurndownService({codeBlockStyle : 'fenced'});
var gfm = turndownPluginGfm.gfm
turndownService.use(gfm)

var tmpclean=getSelectionAsCleanHtml();
this.selection_markdown = turndownService.turndown(
tmpclean
Expand Down
4 changes: 2 additions & 2 deletions lib/turndown-plugin-gfm.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ function highlightedCodeBlock (turndownService) {
var language = (className.match(highlightRegExp) || [null, ''])[1];

return (
'\n\n' + options.fence + language + '\n' +
'#+begin_src ' + language + '\n' +
node.firstChild.textContent +
'\n' + options.fence + '\n\n'
'\n' + '#+end_src\n'
)
}
});
Expand Down
79 changes: 47 additions & 32 deletions lib/turndown.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,22 @@ var TurndownService = (function () {
rules.indentedCodeBlock = {
filter: function (node, options) {
return (
//options.codeBlockStyle === 'indented' &&
//node.nodeName === 'PRE' &&
node.nodeName === 'KBD' &&
options.codeBlockStyle === 'indented' &&
node.nodeName === 'PRE'
//node.firstChild &&
node.firstChild.nodeName === 'CODE'
//node.firstChild.nodeName === 'CODE'
)
},

replacement: function (content, node, options) {
return (
'#+begin_src\n\n ' +
node.firstChild.textContent.replace(/\n/g, '\n ') +
'\n\n#+end_src'
const firstChild = node.firstChild;
if (!firstChild) { return 'ERROR: colonCodeBlock: has no firstChild' }
const textContent = firstChild.textContent;
if (!textContent) { return 'ERROR: colonCodeBlock: firstChild has no textContent' }
return (
'\n\n' +
textContent.replace(/\n/g, '\n: ') +
'\n\n'
)
}
};
Expand All @@ -181,34 +184,35 @@ var TurndownService = (function () {
filter: function (node, options) {
return (
options.codeBlockStyle === 'fenced' &&
node.nodeName === 'PRE' &&
node.firstChild &&
node.firstChild.nodeName === 'CODE'
node.nodeName === 'PRE'
)
},

replacement: function (content, node, options) {
var className = node.firstChild.getAttribute('class') || '';
var language = (className.match(/language-(\S+)/) || [null, ''])[1];
var code = node.firstChild.textContent;

var fenceChar = options.fence.charAt(0);
var fenceSize = 3;
var fenceInCodeRegex = new RegExp('^' + fenceChar + '{3,}', 'gm');

var match;
while ((match = fenceInCodeRegex.exec(code))) {
if (match[0].length >= fenceSize) {
fenceSize = match[0].length + 1;
}
}

var fence = repeat(fenceChar, fenceSize);

// var className = node.firstChild.getAttribute('class') || ''
// var language = (className.match(/language-(\S+)/) || [null, ''])[1]
// var code = node.firstChild.textContent

// var fenceChar = options.fence.charAt(0)
// var fenceSize = 3
// var fenceInCodeRegex = new RegExp('^' + fenceChar + '{3,}', 'gm')

// var match
// while ((match = fenceInCodeRegex.exec(code))) {
// if (match[0].length >= fenceSize) {
// fenceSize = match[0].length + 1
// }
// }
// var fence = repeat(fenceChar, fenceSize)

const textContent = node.innerText;
if (!textContent === null) { return 'ERROR: beginEndCodeBlock: firstChild has no textContent' }
let langId =(node.className.match(/src-(\S+)/) || node.className.match(/language-(\S+)/) || [null, ''])[1];
if (langId) { langId = ' ' + langId; }
return (
'#+begin_src\n\n' + fence + language + '\n' +
code.replace(/\n$/, '') +
'\n' + fence + '\n\n#+end_src'
'#+begin_src ' + langId+ '\n' +
textContent +
'\n' + '#+end_src\n'
)
}
};
Expand Down Expand Up @@ -308,7 +312,7 @@ var TurndownService = (function () {
var hasSiblings = node.previousSibling || node.nextSibling;
var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings;

return node.nodeName === 'CODE' && !isCodeBlock
return (node.nodeName === 'CODE' || node.nodeName === 'KBD') && !isCodeBlock
},

replacement: function (content) {
Expand Down Expand Up @@ -336,6 +340,17 @@ var TurndownService = (function () {
}
};

// rules.checkboxInList = {
// filter: function (node) {
// const el = node as unknown as HTMLInputElement
// return el.type === 'checkbox' && !!el.parentNode && el.parentNode.nodeName === 'LI'
// },
// replacement: function (content, node) {
// const el = node as unknown as HTMLInputElement
// return (el.checked ? '[X]' : '[ ]') + ' '
// }
// }

function cleanAttribute (attribute) {
return attribute ? attribute.replace(/(\n+\s*)+/g, '\n') : ''
}
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Org roam Capture",
"version": "0.3.1",
"version": "0.3.2",
"description": "A helper for capturing things via org-protocol in emacs: First, set up: http://orgmode.org/worg/org-contrib/org-protocol.html",
"homepage_url": "https://github.com/xiliuya/org-roam-capture-extension",

Expand Down
79 changes: 47 additions & 32 deletions test/turndown.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,22 @@ var TurndownService = (function () {
rules.indentedCodeBlock = {
filter: function (node, options) {
return (
//options.codeBlockStyle === 'indented' &&
//node.nodeName === 'PRE' &&
node.nodeName === 'KBD' &&
options.codeBlockStyle === 'indented' &&
node.nodeName === 'PRE'
//node.firstChild &&
node.firstChild.nodeName === 'CODE'
//node.firstChild.nodeName === 'CODE'
)
},

replacement: function (content, node, options) {
return (
'#+begin_src\n\n ' +
node.firstChild.textContent.replace(/\n/g, '\n ') +
'\n\n#+end_src'
const firstChild = node.firstChild;
if (!firstChild) { return 'ERROR: colonCodeBlock: has no firstChild' }
const textContent = firstChild.textContent;
if (!textContent) { return 'ERROR: colonCodeBlock: firstChild has no textContent' }
return (
'\n\n' +
textContent.replace(/\n/g, '\n: ') +
'\n\n'
)
}
};
Expand All @@ -181,34 +184,35 @@ var TurndownService = (function () {
filter: function (node, options) {
return (
options.codeBlockStyle === 'fenced' &&
node.nodeName === 'PRE' &&
node.firstChild &&
node.firstChild.nodeName === 'CODE'
node.nodeName === 'PRE'
)
},

replacement: function (content, node, options) {
var className = node.firstChild.getAttribute('class') || '';
var language = (className.match(/language-(\S+)/) || [null, ''])[1];
var code = node.firstChild.textContent;

var fenceChar = options.fence.charAt(0);
var fenceSize = 3;
var fenceInCodeRegex = new RegExp('^' + fenceChar + '{3,}', 'gm');

var match;
while ((match = fenceInCodeRegex.exec(code))) {
if (match[0].length >= fenceSize) {
fenceSize = match[0].length + 1;
}
}

var fence = repeat(fenceChar, fenceSize);

// var className = node.firstChild.getAttribute('class') || ''
// var language = (className.match(/language-(\S+)/) || [null, ''])[1]
// var code = node.firstChild.textContent

// var fenceChar = options.fence.charAt(0)
// var fenceSize = 3
// var fenceInCodeRegex = new RegExp('^' + fenceChar + '{3,}', 'gm')

// var match
// while ((match = fenceInCodeRegex.exec(code))) {
// if (match[0].length >= fenceSize) {
// fenceSize = match[0].length + 1
// }
// }
// var fence = repeat(fenceChar, fenceSize)

const textContent = node.innerText;
if (!textContent === null) { return 'ERROR: beginEndCodeBlock: firstChild has no textContent' }
let langId =(node.className.match(/src-(\S+)/) || node.className.match(/language-(\S+)/) || [null, ''])[1];
if (langId) { langId = ' ' + langId; }
return (
'#+begin_src\n\n' + fence + language + '\n' +
code.replace(/\n$/, '') +
'\n' + fence + '\n\n#+end_src'
'#+begin_src ' + langId+ '\n' +
textContent +
'\n' + '#+end_src\n'
)
}
};
Expand Down Expand Up @@ -308,7 +312,7 @@ var TurndownService = (function () {
var hasSiblings = node.previousSibling || node.nextSibling;
var isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings;

return node.nodeName === 'CODE' && !isCodeBlock
return (node.nodeName === 'CODE' || node.nodeName === 'KBD') && !isCodeBlock
},

replacement: function (content) {
Expand Down Expand Up @@ -336,6 +340,17 @@ var TurndownService = (function () {
}
};

// rules.checkboxInList = {
// filter: function (node) {
// const el = node as unknown as HTMLInputElement
// return el.type === 'checkbox' && !!el.parentNode && el.parentNode.nodeName === 'LI'
// },
// replacement: function (content, node) {
// const el = node as unknown as HTMLInputElement
// return (el.checked ? '[X]' : '[ ]') + ' '
// }
// }

function cleanAttribute (attribute) {
return attribute ? attribute.replace(/(\n+\s*)+/g, '\n') : ''
}
Expand Down
2 changes: 1 addition & 1 deletion turndown
Submodule turndown updated from 940b8d to 8c8755
2 changes: 1 addition & 1 deletion turndown-plugin-gfm
Submodule turndown-plugin-gfm updated from 61a981 to 463aa5

0 comments on commit 042ba2c

Please sign in to comment.