Skip to content
This repository was archived by the owner on Feb 6, 2024. It is now read-only.

Commit 82b8bb4

Browse files
fix: highlight-lines provided with node.meta (#45)
1 parent 47bf8db commit 82b8bb4

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
### Fix
44

55
- no language provided results in an `undefined` value for the attribute highlight-lines ([#40](https://github.com/deckgo/gatsby-remark-highlight-code/issues/40))
6+
- highlight-lines information might be provided with the `node.lang` but als with `node.meta`
67

78
# 3.0.0 (2021-05-22)
89

tests/mocks.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ const typescriptLangNode = {
1010
const typescriptWithLinesGroupNode = {
1111
lang: "typescript{3,4, 5-9, 22-45}",
1212
};
13+
const dartLangMetaNode = {
14+
lang: "dart{3",
15+
meta: ",4,5}",
16+
};
1317

1418
module.exports = {
1519
dartLangNode,
1620
dartLangWithSpacesNode,
1721
typescriptLangNode,
1822
typescriptWithLinesGroupNode,
23+
dartLangMetaNode,
1924
};

utils.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ const _ = require(`lodash`);
77
* which is compatible with the <deckdeckgo-highlight-code> component (https://docs.deckdeckgo.com/?path=/story/components-highlight-code--highlight-code)
88
* @param {Markdown Node} node
99
*/
10-
const parseLanguageAndHighlightedLines = (node) => {
10+
const parseLanguageAndHighlightedLines = ({lang: nodeLang, meta}) => {
1111
const highlightLinesRegex = /{(.*?)}/g;
12-
let lang = node.lang;
12+
13+
const joinedNodeLang = `${nodeLang}${meta !== null && meta !== undefined ? meta : ''}`
14+
15+
let lang = joinedNodeLang;
1316
let highlightLines = "";
14-
const regexExecResults = highlightLinesRegex.exec(node.lang);
17+
const regexExecResults = highlightLinesRegex.exec(joinedNodeLang);
18+
1519
if (!regexExecResults) { // no lines to highlight
1620
return {
1721
lang,

utils.test.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const {
22
dartLangNode,
33
dartLangWithSpacesNode,
44
typescriptLangNode,
5-
typescriptWithLinesGroupNode,
5+
typescriptWithLinesGroupNode, dartLangMetaNode,
66
} = require("./tests/mocks");
77
const { parseLanguageAndHighlightedLines, parseNodeHtml } = require("./utils");
88

@@ -106,5 +106,8 @@ describe("languages extraction", () => {
106106
parseLanguageAndHighlightedLines(typescriptWithLinesGroupNode)
107107
.highlightLines
108108
).toBe("3 4 5,9 22,45");
109+
expect(parseLanguageAndHighlightedLines(dartLangMetaNode).highlightLines).toBe(
110+
"3 4 5"
111+
);
109112
});
110113
});

0 commit comments

Comments
 (0)