Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[hl_lines] Add a class to code container element
- Loading branch information
1 parent
ea21dd5
commit 94c36bf
Showing
6 changed files
with
51 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* This plugin adds a class precising the language | ||
* to code blocks (.hljs-code-div). | ||
*/ | ||
|
||
const visit = require('unist-util-visit') | ||
|
||
module.exports = () => (tree) => { | ||
visit(tree, (node) => { | ||
// filter super code blocks | ||
if (node.type === 'element' && | ||
node.tagName === 'div' && | ||
node.properties && | ||
node.properties.className && | ||
node.properties.className.includes('hljs-code-div') | ||
) { | ||
// get pre > code | ||
const preElem = node.children.filter(c => | ||
c.type === 'element' && c.tagName === 'pre') | ||
|
||
if (preElem.length === 1) { | ||
const classes = preElem[0].children[0].properties.className | ||
const langClass = classes.filter(c => c.startsWith('language')) | ||
const language = langClass[0].substring(9) | ||
|
||
// rewrite class name | ||
if (langClass.length === 1 && language !== 'div') { | ||
node.properties.className.push(`hljs-code-${language}`) | ||
} | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters