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

Commit ac89cd4

Browse files
Merge pull request #35 from deckgo/fix/language-attributes
fix: ensure no empty language attribute
2 parents 83a8197 + bfade2f commit ac89cd4

File tree

3 files changed

+139
-73
lines changed

3 files changed

+139
-73
lines changed

index.js

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,15 @@
11
const visit = require("unist-util-visit");
2-
const toString = require("mdast-util-to-string");
32

4-
const _ = require(`lodash`);
5-
const { parseLanguageAndHighlightedLines } = require('./utils');
3+
const { parseNodeHtml } = require("./utils");
64

75
module.exports = ({ markdownAST }, pluginOptions) => {
86
visit(markdownAST, "code", (node) => {
9-
let lang = '', highlightLines;
10-
if (node && node.lang !== null) {
11-
({ lang, highlightLines } = parseLanguageAndHighlightedLines(node));
12-
}
13-
const text = toString(node);
14-
const properties = generatePropsString(pluginOptions);
15-
const html = `
16-
<deckgo-highlight-code language=${lang} ${properties} highlight-lines="${highlightLines}">
17-
<code slot="code">${_.escape(text)}</code>
18-
</deckgo-highlight-code>
19-
`.trim();
7+
const html = parseNodeHtml(node, pluginOptions);
8+
209
node.type = "html";
2110
node.children = undefined;
2211
node.value = html;
2312
});
2413

2514
return markdownAST;
2615
};
27-
28-
function generatePropsString(pluginOptions) {
29-
if (!pluginOptions) {
30-
return "";
31-
}
32-
33-
let str = "";
34-
const { terminal, lineNumbers, editable, theme } = pluginOptions;
35-
36-
if (terminal) {
37-
str += `terminal="${pluginOptions.terminal}" `;
38-
}
39-
40-
if (theme) {
41-
str += `theme="${pluginOptions.theme}" `;
42-
}
43-
44-
if (lineNumbers === true) {
45-
str += `line-numbers="true" `;
46-
}
47-
48-
if (editable === true) {
49-
str += `editable="true" `;
50-
}
51-
52-
return str;
53-
}

utils.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
const toString = require("mdast-util-to-string");
2+
const _ = require(`lodash`);
3+
14
/**
25
* Returns the parsed language and the highlighted lines.
36
* For example, ```dart{3, 2, 5-9} will output {lang: 'dart', highlightLines: '3 2 5,9'}
@@ -37,6 +40,52 @@ const parseLanguageAndHighlightedLines = (node) => {
3740
};
3841
};
3942

43+
function generatePropsString(pluginOptions) {
44+
if (!pluginOptions) {
45+
return "";
46+
}
47+
48+
let str = "";
49+
const { terminal, lineNumbers, editable, theme } = pluginOptions;
50+
51+
if (terminal) {
52+
str += `terminal="${pluginOptions.terminal}" `;
53+
}
54+
55+
if (theme) {
56+
str += `theme="${pluginOptions.theme}" `;
57+
}
58+
59+
if (lineNumbers === true) {
60+
str += `line-numbers="true" `;
61+
}
62+
63+
if (editable === true) {
64+
str += `editable="true" `;
65+
}
66+
67+
return str;
68+
}
69+
70+
function parseNodeHtml(node, pluginOptions) {
71+
let lang = "",
72+
highlightLines;
73+
if (node && node.lang !== null) {
74+
({ lang, highlightLines } = parseLanguageAndHighlightedLines(node));
75+
}
76+
const text = toString(node);
77+
const properties = generatePropsString(pluginOptions);
78+
79+
const renderLang = lang !== '' ? `language="${lang}"` : '';
80+
const renderHighlightLines = highlightLines !== '' ? `highlight-lines="${highlightLines}"` : '';
81+
82+
return `<deckgo-highlight-code ${renderLang} ${properties} ${renderHighlightLines}>
83+
<code slot="code">${_.escape(text)}</code>
84+
</deckgo-highlight-code>
85+
`.trim();
86+
}
87+
4088
module.exports = {
4189
parseLanguageAndHighlightedLines,
90+
parseNodeHtml,
4291
};

utils.test.js

Lines changed: 87 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,94 @@ const {
33
dartLangWithSpacesNode,
44
typescriptLangNode,
55
typescriptWithLinesGroupNode,
6-
} = require('./tests/mocks')
7-
const { parseLanguageAndHighlightedLines } = require("./utils");
8-
9-
/**
10-
* Tests for Utils.js
11-
*/
12-
test("detects the languages correctly", () => {
13-
expect(
14-
parseLanguageAndHighlightedLines(dartLangNode).lang
15-
).toBe('dart');
16-
expect(
17-
parseLanguageAndHighlightedLines(dartLangWithSpacesNode).lang
18-
).toBe('dart');
19-
expect(
20-
parseLanguageAndHighlightedLines(typescriptLangNode).lang
21-
).toBe('typescript');
22-
expect(
23-
parseLanguageAndHighlightedLines(typescriptWithLinesGroupNode).lang
24-
).toBe('typescript');
6+
} = require("./tests/mocks");
7+
const { parseLanguageAndHighlightedLines, parseNodeHtml } = require("./utils");
8+
9+
describe("parse node to html", () => {
10+
it("should parse no language and no highlight lines (default)", () => {
11+
const html = parseNodeHtml(
12+
{
13+
text: "<code>Hello World</code>",
14+
lang: "",
15+
},
16+
{}
17+
);
18+
19+
expect(html).toEqual(`<deckgo-highlight-code >
20+
<code slot="code"></code>
21+
</deckgo-highlight-code>`);
22+
});
23+
24+
it("should parse javascript", () => {
25+
const html = parseNodeHtml(
26+
{
27+
text: "<code>Hello World</code>",
28+
lang: "javascript",
29+
},
30+
{}
31+
);
32+
33+
expect(html).toEqual(`<deckgo-highlight-code language="javascript" >
34+
<code slot="code"></code>
35+
</deckgo-highlight-code>`);
36+
});
37+
38+
it("should parse a specific language", () => {
39+
const html = parseNodeHtml(
40+
{
41+
text: "<code>Hello World</code>",
42+
lang: "typescript",
43+
},
44+
{}
45+
);
46+
47+
expect(html).toEqual(`<deckgo-highlight-code language="typescript" >
48+
<code slot="code"></code>
49+
</deckgo-highlight-code>`);
50+
});
51+
52+
it("should parse highlighted lines", () => {
53+
const html = parseNodeHtml(
54+
{
55+
text: "<code>Hello World</code>",
56+
lang: "dart{3, 2, 5-9}",
57+
},
58+
{}
59+
);
60+
61+
expect(html).toEqual(`<deckgo-highlight-code language="dart" highlight-lines="3 2 5,9">
62+
<code slot="code"></code>
63+
</deckgo-highlight-code>`);
64+
});
2565
});
2666

67+
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+
});
2780

28-
test("detects the highlight-lines correctly", () => {
29-
expect(
30-
parseLanguageAndHighlightedLines(dartLangNode).highlightLines
31-
).toBe('3 4 5');
32-
expect(
33-
parseLanguageAndHighlightedLines(dartLangWithSpacesNode).highlightLines
34-
).toBe('5 9 34 39,44');
35-
expect(
36-
parseLanguageAndHighlightedLines(typescriptLangNode).highlightLines
37-
).toBe('');
38-
expect(
39-
parseLanguageAndHighlightedLines(typescriptWithLinesGroupNode).highlightLines
40-
).toBe('3 4 5,9 22,45');
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+
});
4196
});

0 commit comments

Comments
 (0)