Add highlighted code block rules

This commit is contained in:
Dom Christie 2017-11-24 11:19:37 +00:00
parent a4ef6870b7
commit 611f0f037c
3 changed files with 55 additions and 2 deletions

View File

@ -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 }

View 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'
)
}
})
}

View File

@ -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>&lt;<span class="pl-ent">p</span>&gt;Hello world&lt;/<span class="pl-ent">p</span>&gt;</pre>
</div>
</div>
<pre class="expected">```html
&lt;p&gt;Hello world&lt;/p&gt;
```</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>