mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-08-10 10:22:29 +08:00
Add highlighted code block rules
This commit is contained in:
parent
a4ef6870b7
commit
611f0f037c
10
src/gfm.js
10
src/gfm.js
@ -1,9 +1,15 @@
|
||||
import highlightedCodeBlock from './highlighted-code-block'
|
||||
import strikethrough from './strikethrough'
|
||||
import tables from './tables'
|
||||
import taskListItems from './task-list-items'
|
||||
|
||||
function gfm (turndownService) {
|
||||
turndownService.use([strikethrough, tables, taskListItems])
|
||||
turndownService.use([
|
||||
highlightedCodeBlock,
|
||||
strikethrough,
|
||||
tables,
|
||||
taskListItems
|
||||
])
|
||||
}
|
||||
|
||||
export { gfm, strikethrough, tables, taskListItems }
|
||||
export { gfm, highlightedCodeBlock, strikethrough, tables, taskListItems }
|
||||
|
25
src/highlighted-code-block.js
Normal file
25
src/highlighted-code-block.js
Normal file
@ -0,0 +1,25 @@
|
||||
var highlightRegExp = /highlight-(?:text|source)-([a-z0-9]+)/
|
||||
|
||||
export default function highlightedCodeBlock (turndownService) {
|
||||
turndownService.addRule('highlightedCodeBlock', {
|
||||
filter: function (node) {
|
||||
var firstChild = node.firstChild
|
||||
return (
|
||||
node.nodeName === 'DIV' &&
|
||||
highlightRegExp.test(node.className) &&
|
||||
firstChild &&
|
||||
firstChild.nodeName === 'PRE'
|
||||
)
|
||||
},
|
||||
replacement: function (content, node, options) {
|
||||
var className = node.className || ''
|
||||
var language = (className.match(highlightRegExp) || [null, ''])[1]
|
||||
|
||||
return (
|
||||
'\n\n' + options.fence + language + '\n' +
|
||||
node.firstChild.textContent +
|
||||
'\n' + options.fence + '\n\n'
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
@ -273,6 +273,28 @@
|
||||
| --- |</pre>
|
||||
</div>
|
||||
|
||||
<div class="case" data-name="highlighted code block with html">
|
||||
<div class="input">
|
||||
<div class="highlight highlight-text-html-basic">
|
||||
<pre><<span class="pl-ent">p</span>>Hello world</<span class="pl-ent">p</span>></pre>
|
||||
</div>
|
||||
</div>
|
||||
<pre class="expected">```html
|
||||
<p>Hello world</p>
|
||||
```</pre>
|
||||
</div>
|
||||
|
||||
<div class="case" data-name="highlighted code block with js">
|
||||
<div class="input">
|
||||
<div class="highlight highlight-source-js">
|
||||
<pre>;(<span class="pl-k">function</span> () {})()</pre>
|
||||
</div>
|
||||
</div>
|
||||
<pre class="expected">```js
|
||||
;(function () {})()
|
||||
```</pre>
|
||||
</div>
|
||||
|
||||
<!-- /TEST CASES -->
|
||||
|
||||
<script src="turndown-plugin-gfm-test.browser.js"></script>
|
||||
|
Loading…
x
Reference in New Issue
Block a user