Skip to content

Commit f58c53a

Browse files
elevatebartkoca
authored andcommitted
fix: make sure the language class is kept after first render (#31)
* fix: make sure the language class is kept after first render closes #30 * test: update tests to pass
1 parent 2a96fdc commit f58c53a

File tree

4 files changed

+11
-11
lines changed

4 files changed

+11
-11
lines changed

src/utils/prism.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/* global Prism */
22
import escapeHtml from "escape-html";
33

4-
function wrap(code, lang) {
4+
function wrap(code, lang, langPrism) {
55
if (lang === "text") {
66
code = escapeHtml(code);
77
}
8-
return `<code>${code}</code>`;
8+
return `<code class="language-${langPrism}">${code}</code>`;
99
}
1010

1111
export default (str, lang) => {
1212
if (!lang) {
13-
return wrap(str, "text");
13+
return wrap(str, "text", "text");
1414
}
1515
lang = lang.toLowerCase();
1616
const rawLang = lang;
@@ -25,7 +25,7 @@ export default (str, lang) => {
2525
}
2626
if (Prism.languages[lang]) {
2727
const code = Prism.highlight(str, Prism.languages[lang], lang);
28-
return wrap(code, rawLang);
28+
return wrap(code, rawLang, lang);
2929
}
30-
return wrap(str, "text");
30+
return wrap(str, "text", "text");
3131
};

tests/unit/Editor.spec.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ describe("Editor.vue", () => {
104104
sync: false
105105
});
106106

107-
expect(wrapper.vm.content).toBe(
108-
`<code><span class="token function">log</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>`
107+
expect(wrapper.vm.content).toMatchInlineSnapshot(
108+
`<code class="language-js"><span class="token function">log</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>`
109109
);
110110
});
111111
});

tests/unit/__snapshots__/Editor.spec.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
exports[`Editor.vue renders with null value 1`] = `
44
<div class="prism-editor-wrapper">
5-
<!----> <pre contenteditable="true" spellcheck="false" autocapitalize="off" autocomplete="off" autocorrect="off" data-gramm="false" class="prism-editor__code language-js"><code></code></pre>
5+
<!----> <pre contenteditable="true" spellcheck="false" autocapitalize="off" autocomplete="off" autocorrect="off" data-gramm="false" class="prism-editor__code language-js"><code class="language-js"></code></pre>
66
</div>
77
`;

tests/unit/utils/prism.spec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import "prismjs";
22
import prism from "@/utils/prism";
33
const input = 'var x = "Hello World!";';
4-
const expected =
5-
'<code><span class="token keyword">var</span> x <span class="token operator">=</span> <span class="token string">"Hello World!"</span><span class="token punctuation">;</span></code>';
64

75
describe("prism", () => {
86
it("should highlight js(x) code", () => {
9-
expect(prism(input, "js")).toBe(expected);
7+
expect(prism(input, "js")).toMatchInlineSnapshot(
8+
`<code class="language-js"><span class="token keyword">var</span> x <span class="token operator">=</span> <span class="token string">"Hello World!"</span><span class="token punctuation">;</span></code>`
9+
);
1010
});
1111
});

0 commit comments

Comments
 (0)