Skip to content

Commit 05aa66a

Browse files
committed
fix pre[data-language]
1 parent 7816a31 commit 05aa66a

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

docs/kit.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
You can install Notebook Kit from [npm](https://www.npmjs.com/package/@observablehq/notebook-kit):
2424

25-
```bash
25+
```sh
2626
npm install @observablehq/notebook-kit
2727
```
2828

@@ -51,17 +51,17 @@
5151
<!doctype html>
5252
<notebook>
5353
<title>Hello, world!</title>
54-
<script id="1" type="text/markdown">
54+
<script type="text/markdown">
5555
# Hello, world!
5656
<\/script>
57-
<script id="2" type="module" pinned>
57+
<script type="module" pinned>
5858
1 + 2
5959
<\/script>
6060
</notebook>
6161
```
6262
</script>
6363
<script id="60" type="text/markdown">
64-
The cell <code class="language-html">&lt;script&gt;</code> element should use four spaces of indentation for each line; these four leading spaces will be trimmed when parsing the notebook. (In the future, we'll likely make this contextual to allow more or less indentation.)
64+
The cell <code class="language-html">&lt;script&gt;</code> element should use four spaces of indentation for each line; these four leading spaces are trimmed when parsing the notebook. (In the future, we'll likely make this contextual to allow varied indentation.)
6565
</script>
6666
<script id="49" type="text/markdown">
6767
The `type` attribute determines each cell's mode, which can be one of:
@@ -165,7 +165,7 @@ <h1>Hello, <i>world</i>!</h1>
165165
- `stark`
166166
- `sun-faded`
167167

168-
See [Observable Framework's themes](https://observablehq.com/framework/themes) for a visual overview of themes. More themes may be added in the future, and custom themes can be designed using standard stylesheets.
168+
See [Observable Framework's themes](https://observablehq.com/framework/themes) for a visual overview. More themes may be added in the future, and custom themes can be applied using standard stylesheets.
169169
</script>
170170
<script id="56" type="text/markdown">
171171
See [`notebook.ts`](https://github.com/observablehq/notebook-kit/blob/main/src/lib/notebook.ts) for TypeScript types representing notebooks and cells.

src/runtime/stdlib/highlight.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ export const highlighter = tagHighlighter([
2020
]);
2121

2222
export async function highlight(code: HTMLElement): Promise<void> {
23-
const parser = await getParser(code);
23+
const language = getLanguage(code);
24+
if (!language) return;
25+
26+
const parent = code.parentElement;
27+
if (parent) parent.dataset.language = language;
28+
29+
const parser = await getParser(language);
2430
if (!parser) return;
2531

2632
const document = code.ownerDocument;
@@ -46,15 +52,12 @@ export async function highlight(code: HTMLElement): Promise<void> {
4652
highlightCode(text, tree, highlighter, emit, emitBreak);
4753
}
4854

49-
async function getParser(code: HTMLElement): Promise<Parser | undefined> {
50-
const dialect = getLanguage(code);
51-
switch (dialect) {
55+
async function getParser(language: string): Promise<Parser | undefined> {
56+
switch (language) {
5257
case "js":
5358
case "ts":
5459
case "jsx":
55-
case "javascript":
56-
case "typescript":
57-
return (await import("@lezer/javascript")).parser.configure({dialect});
60+
return (await import("@lezer/javascript")).parser.configure({dialect: language});
5861
case "html":
5962
return (await import("@lezer/html")).parser;
6063
case "css":
@@ -66,8 +69,15 @@ async function getParser(code: HTMLElement): Promise<Parser | undefined> {
6669
}
6770

6871
function getLanguage(code: HTMLElement): string | undefined {
69-
return [...code.classList]
72+
const language = [...code.classList]
7073
.find((c) => c.startsWith("language-"))
7174
?.slice("language-".length)
7275
?.toLowerCase();
76+
switch (language) {
77+
case "javascript":
78+
return "js";
79+
case "typescript":
80+
return "ts";
81+
}
82+
return language;
7383
}

src/vite/observable.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,6 @@ export function observable({
8989
code.textContent = value;
9090
await highlight(code);
9191
}
92-
for (const pre of contents.querySelectorAll("pre")) {
93-
const child = pre.firstElementChild;
94-
if (child?.tagName !== "CODE") continue;
95-
const language = getLanguage(child);
96-
if (!language) continue;
97-
pre.dataset.language = language;
98-
}
9992
cells.appendChild(contents);
10093
}
10194

0 commit comments

Comments
 (0)