Skip to content

Commit d676b17

Browse files
committed
Somewhat get inline code syntax highlighting to work
1 parent 97990fe commit d676b17

File tree

7 files changed

+163
-8
lines changed

7 files changed

+163
-8
lines changed

astro.config.ts

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import starlightSidebarTopics from "starlight-sidebar-topics";
77
import miniMessageHighlight from "./src/assets/mm.tmLanguage.json";
88
import codeConstantsPlugin from "./src/utils/remark/code_const";
99
import javadocPlugin from "./src/utils/remark/javadoc";
10-
import { LATEST_MC_RELEASE, LATEST_PAPER_RELEASE, LATEST_VELOCITY_RELEASE } from "./src/utils/versions";
10+
import {
11+
LATEST_MC_RELEASE,
12+
LATEST_PAPER_RELEASE,
13+
LATEST_VELOCITY_RELEASE,
14+
} from "./src/utils/versions";
1115

1216
const prod = process.env.NODE_ENV === "production";
1317

@@ -50,7 +54,8 @@ export default defineConfig({
5054
Footer: "./src/components/overrides/Footer.astro",
5155
Banner: "./src/components/overrides/Banner.astro",
5256
TableOfContents: "./src/components/overrides/TableOfContents.astro",
53-
MobileTableOfContents: "./src/components/overrides/MobileTableOfContents.astro",
57+
MobileTableOfContents:
58+
"./src/components/overrides/MobileTableOfContents.astro",
5459
},
5560
plugins: [
5661
starlightLinksValidator({
@@ -69,7 +74,12 @@ export default defineConfig({
6974
items: [
7075
{
7176
label: "Getting started",
72-
items: ["paper/getting-started", "paper/adding-plugins", "paper/migration", "paper/next-steps"],
77+
items: [
78+
"paper/getting-started",
79+
"paper/adding-plugins",
80+
"paper/migration",
81+
"paper/next-steps",
82+
],
7383
},
7484
{
7585
label: "How-to guides",
@@ -186,7 +196,10 @@ export default defineConfig({
186196
{
187197
label: "Entity API",
188198
collapsed: true,
189-
items: ["paper/dev/entity-teleport", "paper/dev/display-entities"],
199+
items: [
200+
"paper/dev/entity-teleport",
201+
"paper/dev/display-entities",
202+
],
190203
},
191204
"paper/dev/data-component-api",
192205
"paper/dev/pdc",
@@ -238,7 +251,11 @@ export default defineConfig({
238251
},
239252
{
240253
label: "How-to guides",
241-
items: ["velocity/tuning", "velocity/security", "velocity/migration"],
254+
items: [
255+
"velocity/tuning",
256+
"velocity/security",
257+
"velocity/migration",
258+
],
242259
},
243260
{
244261
label: "Reference",
@@ -266,7 +283,10 @@ export default defineConfig({
266283
},
267284
{
268285
label: "How-to guides",
269-
items: ["velocity/dev/dependency-management", "velocity/dev/porting-plugins-from-velocity-1"],
286+
items: [
287+
"velocity/dev/dependency-management",
288+
"velocity/dev/porting-plugins-from-velocity-1",
289+
],
270290
},
271291
{
272292
label: "API",
@@ -301,7 +321,11 @@ export default defineConfig({
301321
items: [
302322
{
303323
label: "Reference",
304-
items: ["folia/reference/overview", "folia/reference/region-logic", "folia/faq"],
324+
items: [
325+
"folia/reference/overview",
326+
"folia/reference/region-logic",
327+
"folia/faq",
328+
],
305329
},
306330
],
307331
},
@@ -333,6 +357,7 @@ export default defineConfig({
333357
"adventure/minimessage/api",
334358
"adventure/minimessage/dynamic-replacements",
335359
"adventure/minimessage/translator",
360+
"adventure/minimessage/ultra-small",
336361
],
337362
},
338363
"adventure/serializer/ansi",

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
"prettier": "3.5.3",
3535
"prettier-plugin-astro": "0.14.1",
3636
"prettier-plugin-organize-imports": "^4.1.0",
37-
"prettier-plugin-svelte": "^3.3.3"
37+
"prettier-plugin-svelte": "^3.3.3",
38+
"shiki": "^3.2.2"
3839
},
3940
"packageManager": "pnpm@10.8.0+sha512.0e82714d1b5b43c74610193cb20734897c1d00de89d0e18420aebc5977fa13d780a9cb05734624e81ebd81cc876cd464794850641c48b9544326b5622ca29971"
4041
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
import { highlight } from "../utils/shiki-utils"
3+
4+
interface Props {
5+
lang: string;
6+
code: string;
7+
}
8+
9+
const props: Props = Astro.props;
10+
const html = highlight(props.code, props.lang);
11+
---
12+
13+
<a set:html={html} />
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: Temp
3+
---
4+
5+
import InlineHighlight from "/src/components/InlineHighlight.astro";
6+
7+
This is <InlineHighlight lang="mm" code="<red>some red text, and <green>now green!" />, and now nothing!
8+
9+
10+
```mm
11+
<gradient:red:'blue'>This is what it <b>should look like</b>!</gradient>
12+
```
13+
<br />
14+
<InlineHighlight lang="mm" code="<gradient:red:'blue'>This is what it <b>should look like</b>!</gradient>" />
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { createHighlighter } from "shiki";
2+
3+
import MiniMessage from "../../assets/mm.tmLanguage.json";
4+
5+
const highlighter = await createHighlighter({
6+
themes: ["one-dark-pro", "one-light"],
7+
langs: ["java", MiniMessage],
8+
});
9+
10+
highlighter.loadLanguage();
11+
12+
const inlineCodeRegex = /:\w+:\`.+\`/g;
13+
const langRegex = /:\w+:/g;
14+
const codeRegex = /\`.+\`/g;
15+
16+
function plugin() {
17+
return function (tree, file) {
18+
console.log(file);
19+
20+
const rawContent = file.value;
21+
let content = rawContent;
22+
23+
let match = null;
24+
do {
25+
match = inlineCodeRegex.exec(content);
26+
if (match) {
27+
const matchContent: string = match[0];
28+
29+
let rawLang: RegExpExecArray | null = langRegex.exec(matchContent);
30+
if (rawLang == null) {
31+
throw "Unexpected error: rawLang is null.";
32+
}
33+
34+
let fullLang = rawLang[0];
35+
let lang: string = fullLang.split(":")[1];
36+
37+
let rawCode: RegExpExecArray | null = codeRegex.exec(matchContent);
38+
if (rawCode == null) {
39+
throw "Unexpected error: rawCode is null.";
40+
}
41+
42+
let fullCode: string = rawCode[0];
43+
let code: string = fullCode.split("`")[1];
44+
45+
// console.log(
46+
// `Lang: ${lang}. Code: ${code}. (FullLang: ${fullLang}. FullCode: ${fullCode}) {${matchContent}}`
47+
// );
48+
49+
const output = highlighter.codeToHtml(code, {
50+
lang: lang,
51+
theme: "one-dark-pro",
52+
}).replace("pre", "a").replace("</pre>", "</a>");
53+
54+
console.log(`Output: ${output}`);
55+
content = content.replaceAll(match, output);
56+
}
57+
} while (match != null);
58+
59+
// console.log(`Old content: ${rawContent}\nNew Content: ${content}`);
60+
file.value = content;
61+
console.log(file);
62+
};
63+
}
64+
65+
export default plugin;

src/utils/shiki-utils.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { createHighlighter } from "shiki";
2+
import MiniMessageLang from "../assets/mm.tmLanguage.json";
3+
4+
// import NightOwlDark from "@astrojs/starlight/integrations/expressive-code/themes/night-owl-dark.jsonc";
5+
// import NightOwlLight from "@astrojs/starlight/integrations/expressive-code/themes/night-owl-light.jsonc";
6+
import starlightConfig from "virtual:starlight/user-config";
7+
8+
let themes: Array<any> | undefined = undefined;
9+
10+
if (typeof starlightConfig.expressiveCode === "object") {
11+
themes = starlightConfig.expressiveCode.themes;
12+
}
13+
14+
// themes = themes ?? ["night-owl", "night-owl"];
15+
themes = ["night-owl", "one-light"]
16+
17+
let highlighter = await createHighlighter({
18+
langs: [MiniMessageLang, "java"],
19+
themes: themes,
20+
});
21+
22+
export const getHighlighter = () => highlighter;
23+
24+
export const highlight = (code: string, lang: string) => {
25+
const html = highlighter.codeToHtml(code, {
26+
lang: lang,
27+
themes: {
28+
dark: themes[1],
29+
light: themes[0],
30+
},
31+
});
32+
33+
return html.replace("pre", "a").replace("</pre>", "</a>");
34+
};

0 commit comments

Comments
 (0)