Skip to content

Commit 99a6905

Browse files
fix: Code block language parsing (#1362)
* fix: Code block language parsing * Small change to code clarity --------- Co-authored-by: matthewlipski <[email protected]>
1 parent ade59fa commit 99a6905

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
[
2+
{
3+
"id": "1",
4+
"type": "codeBlock",
5+
"props": {
6+
"language": "javascript"
7+
},
8+
"content": [
9+
{
10+
"type": "text",
11+
"text": "console.log(\"Should default to JS\")",
12+
"styles": {}
13+
}
14+
],
15+
"children": []
16+
},
17+
{
18+
"id": "2",
19+
"type": "codeBlock",
20+
"props": {
21+
"language": "typescript"
22+
},
23+
"content": [
24+
{
25+
"type": "text",
26+
"text": "console.log(\"Should parse TS from data-language\")",
27+
"styles": {}
28+
}
29+
],
30+
"children": []
31+
},
32+
{
33+
"id": "3",
34+
"type": "codeBlock",
35+
"props": {
36+
"language": "python"
37+
},
38+
"content": [
39+
{
40+
"type": "text",
41+
"text": "print(\"Should parse Python from language- class\")",
42+
"styles": {}
43+
}
44+
],
45+
"children": []
46+
},
47+
{
48+
"id": "4",
49+
"type": "codeBlock",
50+
"props": {
51+
"language": "typescript"
52+
},
53+
"content": [
54+
{
55+
"type": "text",
56+
"text": "console.log(\"Should prioritize TS from data-language over language- class\")",
57+
"styles": {}
58+
}
59+
],
60+
"children": []
61+
}
62+
]

packages/core/src/api/parsers/html/parseHTML.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,4 +516,13 @@ With Hard Break</p>
516516

517517
await parseHTMLAndCompareSnapshots(html, "parse-google-docs-html");
518518
});
519+
520+
it("Parse codeblocks", async () => {
521+
const html = `<pre><code>console.log("Should default to JS")</code></pre>
522+
<pre><code data-language="typescript">console.log("Should parse TS from data-language")</code></pre>
523+
<pre><code class="language-python">print("Should parse Python from language- class")</code></pre>
524+
<pre><code class="language-ruby" data-language="typescript">console.log("Should prioritize TS from data-language over language- class")</code></pre>`;
525+
526+
await parseHTMLAndCompareSnapshots(html, "parse-codeblocks");
527+
});
519528
});

packages/core/src/blocks/CodeBlockContent/CodeBlockContent.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ const CodeBlockContent = createStronglyTypedTiptapNode({
7777
const languages = classNames
7878
.filter((className) => className.startsWith("language-"))
7979
.map((className) => className.replace("language-", ""));
80-
const [classLanguage] = languages;
8180

82-
language = classLanguage.toLowerCase();
81+
if (languages.length > 0) {
82+
language = languages[0].toLowerCase();
83+
}
8384
}
8485

8586
if (!language) {

0 commit comments

Comments
 (0)