Skip to content

Commit 81ba04e

Browse files
keithamuskoddsson
authored andcommitted
improve code-interactive
1 parent a40a7f9 commit 81ba04e

File tree

6 files changed

+105
-43
lines changed

6 files changed

+105
-43
lines changed

.eleventy.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ module.exports = (eleventyConfig) => {
7272
html: true,
7373
linkify: true,
7474
highlight: (str, lang) => {
75-
return `<pre><code-interactive lang="${lang}">${md.utils.escapeHtml(str)}</code-interactive></pre>`
75+
return `<pre class="code-interactive"><code-interactive lang="${lang}">${md.utils.escapeHtml(
76+
str
77+
)}</code-interactive></pre>`
7678
},
7779
})
7880
.use(headers, { prefixHeadingIds: false })

css/code-interactive.css

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
background-color: var(--surface-2);
33
display: block;
44
border-radius: var(--radius-2);
5-
max-inline-size: 100ch;
6-
padding: var(--size-3);
7-
margin-block: var(--size-5);
85
overflow-x: auto;
96
color: var(--text-2);
107

@@ -20,6 +17,7 @@
2017
--token-number: var(--green-6);
2118
--token-comment: var(--teal-6);
2219
--token-function: var(--pink-6);
20+
--token-variable: var(--text-2);
2321
--token-string: var(--cyan-6);
2422
--token-template-string: var(--cyan-6);
2523
--token-interpolation: var(--gray-6);
@@ -29,6 +27,7 @@
2927
@media (prefers-color-scheme: dark) {
3028
:host {
3129
color: var(--cyan-6);
30+
3231
--token-punctuation: var(--indigo-2);
3332
--token-tag: var(--indigo-4);
3433
--token-attr-name: var(--pink-6);
@@ -39,35 +38,42 @@
3938
}
4039
}
4140

41+
/* stylelint-disable selector-class-pattern */
4242
.ͼ1 .cm-scroller {
4343
font-family: var(--font-mono);
44+
padding: var(--size-3);
4445
}
4546

46-
.ͼf {
47-
color: var(--token-template-string);
47+
.ͼb {
48+
color: var(--token-keyword);
4849
}
4950

50-
/* Shared tokens */
51-
.token.punctuation {
52-
color: var(--token-punctuation);
51+
.ͼd {
52+
color: var(--token-number);
5353
}
54-
/* CSS tokens */
55-
.token.selector {
56-
color: var(--token-selector);
54+
55+
.ͼe {
56+
color: var(--token-string);
5757
}
58-
.token.property {
59-
color: var(--token-property);
58+
59+
.ͼf {
60+
color: var(--token-template-string);
6061
}
61-
/* HTML tokens */
62-
.token.tag {
63-
color: var(--token-tag);
62+
63+
.ͼg {
64+
color: var(--token-variable);
6465
}
65-
.token.attr-name {
66-
color: var(--token-attr-name);
66+
67+
.ͼj {
68+
color: var(--token-class-name);
6769
}
68-
.token.attr-value {
69-
color: var(--token-attr-value);
70+
71+
.ͼl {
72+
color: var(--token-function);
7073
}
71-
.token.attr-equals {
72-
color: var(--token-attr-equals);
74+
75+
.ͼm {
76+
color: var(--token-comment);
7377
}
78+
79+
/* stylelint-enable selector-class-pattern */

css/global.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ page-search {
354354
margin-block: var(--size-5);
355355
}
356356

357+
<<<<<<< HEAD
357358
.language-js .keyword {
358359
color: var(--orange-6);
359360
}
@@ -458,3 +459,8 @@ page-search {
458459
.token.attr-equals {
459460
color: var(--token-attr-equals);
460461
}
462+
463+
.code-interactive {
464+
max-inline-size: 100ch;
465+
margin-block: var(--size-5);
466+
}

package-lock.json

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
"@11ty/eleventy-fetch": "^3.0.0",
7575
"@11ty/eleventy-plugin-rss": "^1.2.0",
7676
"@11ty/eleventy-plugin-syntaxhighlight": "^4.2.0",
77+
"@codemirror/lang-css": "^6.0.1",
78+
"@codemirror/lang-html": "^6.4.1",
7779
"@github/relative-time-element": "^4.1.5",
7880
"@jamshop/eleventy-plugin-esbuild": "WebComponentsGuide/eleventy-plugin-esbuild",
7981
"@oddbird/popover-polyfill": "^0.0.8",

script/codemirror.js renamed to script/code-interactive.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,33 @@ import styles from "../css/code-interactive.css"
22

33
import { EditorView } from "codemirror"
44
import { javascript } from "@codemirror/lang-javascript"
5+
import { html } from "@codemirror/lang-html"
6+
import { css } from "@codemirror/lang-css"
57
import {
6-
keymap,
7-
highlightSpecialChars,
8+
crosshairCursor,
89
drawSelection,
9-
highlightActiveLine,
1010
dropCursor,
11+
highlightActiveLine,
12+
highlightSpecialChars,
13+
keymap,
1114
rectangularSelection,
12-
crosshairCursor,
13-
lineNumbers,
14-
highlightActiveLineGutter,
1515
} from "@codemirror/view"
16-
import { EditorState, Transaction } from "@codemirror/state"
16+
import { EditorState } from "@codemirror/state"
1717
import {
18-
defaultHighlightStyle,
19-
syntaxHighlighting,
20-
indentOnInput,
2118
bracketMatching,
22-
foldGutter,
19+
defaultHighlightStyle,
2320
foldKeymap,
21+
indentOnInput,
22+
syntaxHighlighting,
2423
} from "@codemirror/language"
2524
import { defaultKeymap, history, historyKeymap } from "@codemirror/commands"
26-
import { searchKeymap, highlightSelectionMatches } from "@codemirror/search"
27-
import { autocompletion, completionKeymap, closeBrackets, closeBracketsKeymap } from "@codemirror/autocomplete"
25+
import { highlightSelectionMatches, searchKeymap } from "@codemirror/search"
26+
import { autocompletion, closeBrackets, closeBracketsKeymap, completionKeymap } from "@codemirror/autocomplete"
2827
import { lintKeymap } from "@codemirror/lint"
2928

3029
const setup = (() => [
31-
//lineNumbers(),
32-
//highlightActiveLineGutter(),
3330
highlightSpecialChars(),
3431
history(),
35-
//foldGutter(),
3632
drawSelection(),
3733
dropCursor(),
3834
EditorState.allowMultipleSelections.of(true),
@@ -56,14 +52,14 @@ const setup = (() => [
5652
]),
5753
])()
5854

59-
class CodeInteractiveElement extends HTMLElement {
55+
export default class CodeInteractiveElement extends HTMLElement {
6056
static define(tagName = "code-interactive") {
6157
customElements.define(tagName, this)
6258
}
6359

6460
shadowRoot = this.attachShadow({ mode: "open" })
6561
#editor = new EditorView({
66-
extensions: [setup, javascript()],
62+
extensions: [setup, javascript(), html(), css()],
6763
parent: this.shadowRoot,
6864
root: this.shadowRoot,
6965
doc: this.textContent,
@@ -73,5 +69,3 @@ class CodeInteractiveElement extends HTMLElement {
7369
this.shadowRoot.adoptedStyleSheets.push(styles)
7470
}
7571
}
76-
77-
CodeInteractiveElement.define()

0 commit comments

Comments
 (0)