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

Commit 47bf8db

Browse files
fix: no language provided and undefined highlight-lines (#43)
* fix: no language provided and undefined highlight-lines * docs: fix highlight-lines undefined
1 parent 9f96597 commit 47bf8db

File tree

4 files changed

+52
-31
lines changed

4 files changed

+52
-31
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 3.0.1 (2021-07-28)
2+
3+
### Fix
4+
5+
- 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+
17
# 3.0.0 (2021-05-22)
28

39
### Breaking Changes

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ Single or multiple lines of code can be highlighted.
193193
<img src="https://raw.githubusercontent.com/deckgo/gatsby-remark-highlight-code/master/static/highlight-lines.gif" alt="Highlight lines" width="50%">
194194
</div>
195195

196-
The Markdown syntax is the following: next to the specification of the language, between brackets `{}`, the lines should be provided in a comma separated list. A single line can be provided (for example `dart{1}`) or multiple one, from and to being separated with a dash (for example `javascript{3-6}`). Both single or multiple lines can be mixed (for example `typescript{2, 3,4, 7, 8-15}`).
196+
The Markdown syntax is the following: next to the specification of the language, between brackets `{}`, the lines should be provided in a comma separated list. A single line can be provided (for example `dart{1}`) or multiple one, from and to being separated with a dash (for example `javascript{3-6}`). Both single or multiple lines can be mixed (for example `typescript{2, 3-4, 7, 8-15}`).
197197

198198
Animation between the selected highlighted groups can be triggered with the help of methods (see component [@deckdeckgo/highlight-code] documentation for details).
199199

utils.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,16 @@ function generatePropsString(pluginOptions) {
6969

7070
function parseNodeHtml(node, pluginOptions) {
7171
let lang = "",
72-
highlightLines;
72+
highlightLines = undefined;
73+
7374
if (node && node.lang !== null) {
7475
({ lang, highlightLines } = parseLanguageAndHighlightedLines(node));
7576
}
7677
const text = toString(node);
7778
const properties = generatePropsString(pluginOptions);
7879

79-
const renderLang = lang !== '' ? `language="${lang}"` : '';
80-
const renderHighlightLines = highlightLines !== '' ? `highlight-lines="${highlightLines}"` : '';
80+
const renderLang = lang !== '' && lang !== undefined ? `language="${lang}"` : '';
81+
const renderHighlightLines = highlightLines !== '' && highlightLines !== undefined ? `highlight-lines="${highlightLines}"` : '';
8182

8283
return `<deckgo-highlight-code ${renderLang} ${properties} ${renderHighlightLines}>
8384
<code slot="code">${_.escape(text)}</code>

utils.test.js

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -62,35 +62,49 @@ describe("parse node to html", () => {
6262
<code slot="code">hello world</code>
6363
</deckgo-highlight-code>`);
6464
});
65+
66+
it("should parse no language and no highlight lines for null language", () => {
67+
const html = parseNodeHtml(
68+
{
69+
value: "hello world",
70+
lang: null,
71+
},
72+
{}
73+
);
74+
75+
expect(html).toEqual(`<deckgo-highlight-code >
76+
<code slot="code">hello world</code>
77+
</deckgo-highlight-code>`);
78+
});
6579
});
6680

6781
describe("languages extraction", () => {
68-
test("detects the languages correctly", () => {
69-
expect(parseLanguageAndHighlightedLines(dartLangNode).lang).toBe("dart");
70-
expect(parseLanguageAndHighlightedLines(dartLangWithSpacesNode).lang).toBe(
71-
"dart"
72-
);
73-
expect(parseLanguageAndHighlightedLines(typescriptLangNode).lang).toBe(
74-
"typescript"
75-
);
76-
expect(
77-
parseLanguageAndHighlightedLines(typescriptWithLinesGroupNode).lang
78-
).toBe("typescript");
79-
});
82+
test("detects the languages correctly", () => {
83+
expect(parseLanguageAndHighlightedLines(dartLangNode).lang).toBe("dart");
84+
expect(parseLanguageAndHighlightedLines(dartLangWithSpacesNode).lang).toBe(
85+
"dart"
86+
);
87+
expect(parseLanguageAndHighlightedLines(typescriptLangNode).lang).toBe(
88+
"typescript"
89+
);
90+
expect(
91+
parseLanguageAndHighlightedLines(typescriptWithLinesGroupNode).lang
92+
).toBe("typescript");
93+
});
8094

81-
test("detects the highlight-lines correctly", () => {
82-
expect(parseLanguageAndHighlightedLines(dartLangNode).highlightLines).toBe(
83-
"3 4 5"
84-
);
85-
expect(
86-
parseLanguageAndHighlightedLines(dartLangWithSpacesNode).highlightLines
87-
).toBe("5 9 34 39,44");
88-
expect(
89-
parseLanguageAndHighlightedLines(typescriptLangNode).highlightLines
90-
).toBe("");
91-
expect(
92-
parseLanguageAndHighlightedLines(typescriptWithLinesGroupNode)
93-
.highlightLines
94-
).toBe("3 4 5,9 22,45");
95-
});
95+
test("detects the highlight-lines correctly", () => {
96+
expect(parseLanguageAndHighlightedLines(dartLangNode).highlightLines).toBe(
97+
"3 4 5"
98+
);
99+
expect(
100+
parseLanguageAndHighlightedLines(dartLangWithSpacesNode).highlightLines
101+
).toBe("5 9 34 39,44");
102+
expect(
103+
parseLanguageAndHighlightedLines(typescriptLangNode).highlightLines
104+
).toBe("");
105+
expect(
106+
parseLanguageAndHighlightedLines(typescriptWithLinesGroupNode)
107+
.highlightLines
108+
).toBe("3 4 5,9 22,45");
109+
});
96110
});

0 commit comments

Comments
 (0)