diff --git a/src/gfm.js b/src/gfm.js index e70db4649..69d25b4b7 100644 --- a/src/gfm.js +++ b/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 } diff --git a/src/highlighted-code-block.js b/src/highlighted-code-block.js new file mode 100644 index 000000000..97052de57 --- /dev/null +++ b/src/highlighted-code-block.js @@ -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' + ) + } + }) +} diff --git a/test/index.html b/test/index.html index 659e1d0ee..885f738f0 100644 --- a/test/index.html +++ b/test/index.html @@ -273,6 +273,28 @@ | --- | +
<p>Hello world</p>
+ ```html +<p>Hello world</p> +```+
;(function () {})()
+ ```js +;(function () {})() +```+