Skip to content

Commit b6a56d7

Browse files
committed
half migrate to EC
1 parent 2748b9d commit b6a56d7

File tree

4 files changed

+84
-58
lines changed

4 files changed

+84
-58
lines changed

bun.lockb

15.6 KB
Binary file not shown.

exampleVault/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
test: hello
33
---
44

5-
```ts
5+
```ts asdasd
66
export class Parser<const SType extends STypeBase> {
77
public p: ParseFunction<SType>;
88

package.json

Lines changed: 51 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,53 @@
11
{
2-
"name": "shiki-highlighter",
3-
"version": "0.1.2",
4-
"description": "Highlight code blocks with Shiki.",
5-
"main": "main.js",
6-
"scripts": {
7-
"dev": "bun run automation/build/esbuild.dev.config.ts",
8-
"build": "bun run tsc && bun run automation/build/esbuild.config.ts",
9-
"tsc": "tsc -noEmit -skipLibCheck",
10-
"test": "bun test",
11-
"test:log": "LOG_TESTS=true bun test",
12-
"format": "prettier --write --plugin prettier-plugin-svelte .",
13-
"format:check": "prettier --check --plugin prettier-plugin-svelte .",
14-
"lint": "eslint --max-warnings=0 src/**",
15-
"lint:fix": "eslint --max-warnings=0 --fix src/**",
16-
"svelte-check": "svelte-check --compiler-warnings \"unused-export-let:ignore\"",
17-
"check": "bun run format:check && bun run tsc && bun run svelte-check && bun run lint && bun run test",
18-
"check:fix": "bun run format && bun run tsc && bun run svelte-check && bun run lint:fix && bun run test",
19-
"release": "bun run automation/release.ts",
20-
"stats": "bun run automation/stats.ts"
21-
},
22-
"keywords": [],
23-
"author": "Moritz Jung",
24-
"license": "GPL-3.0",
25-
"devDependencies": {
26-
"@happy-dom/global-registrator": "^14.3.6",
27-
"@lemons_dev/parsinom": "^0.0.12",
28-
"@tsconfig/svelte": "^5.0.3",
29-
"@types/bun": "^1.0.10",
30-
"@typescript-eslint/eslint-plugin": "^7.3.1",
31-
"@typescript-eslint/parser": "^7.3.1",
32-
"builtin-modules": "^3.3.0",
33-
"esbuild": "^0.20.2",
34-
"esbuild-plugin-copy-watch": "^2.1.0",
35-
"esbuild-svelte": "^0.8.0",
36-
"eslint": "^8.57.0",
37-
"eslint-plugin-import": "^2.29.1",
38-
"eslint-plugin-isaacscript": "^3.12.2",
39-
"eslint-plugin-only-warn": "^1.1.0",
40-
"obsidian": "latest",
41-
"prettier": "^3.2.5",
42-
"prettier-plugin-svelte": "^3.2.2",
43-
"shiki": "^1.2.0",
44-
"string-argv": "^0.3.2",
45-
"svelte": "^4.2.12",
46-
"svelte-check": "^3.6.8",
47-
"svelte-preprocess": "^5.1.3",
48-
"tslib": "^2.6.2",
49-
"typescript": "^5.4.3"
50-
}
2+
"name": "shiki-highlighter",
3+
"version": "0.1.2",
4+
"description": "Highlight code blocks with Shiki.",
5+
"main": "main.js",
6+
"scripts": {
7+
"dev": "bun run automation/build/esbuild.dev.config.ts",
8+
"build": "bun run tsc && bun run automation/build/esbuild.config.ts",
9+
"tsc": "tsc -noEmit -skipLibCheck",
10+
"test": "bun test",
11+
"test:log": "LOG_TESTS=true bun test",
12+
"format": "prettier --write --plugin prettier-plugin-svelte .",
13+
"format:check": "prettier --check --plugin prettier-plugin-svelte .",
14+
"lint": "eslint --max-warnings=0 src/**",
15+
"lint:fix": "eslint --max-warnings=0 --fix src/**",
16+
"svelte-check": "svelte-check --compiler-warnings \"unused-export-let:ignore\"",
17+
"check": "bun run format:check && bun run tsc && bun run svelte-check && bun run lint && bun run test",
18+
"check:fix": "bun run format && bun run tsc && bun run svelte-check && bun run lint:fix && bun run test",
19+
"release": "bun run automation/release.ts",
20+
"stats": "bun run automation/stats.ts"
21+
},
22+
"keywords": [],
23+
"author": "Moritz Jung",
24+
"license": "GPL-3.0",
25+
"devDependencies": {
26+
"@expressive-code/core": "^0.33.5",
27+
"@expressive-code/plugin-shiki": "^0.33.5",
28+
"@happy-dom/global-registrator": "^14.3.6",
29+
"@lemons_dev/parsinom": "^0.0.12",
30+
"@tsconfig/svelte": "^5.0.3",
31+
"@types/bun": "^1.0.10",
32+
"@typescript-eslint/eslint-plugin": "^7.3.1",
33+
"@typescript-eslint/parser": "^7.3.1",
34+
"builtin-modules": "^3.3.0",
35+
"esbuild": "^0.20.2",
36+
"esbuild-plugin-copy-watch": "^2.1.0",
37+
"esbuild-svelte": "^0.8.0",
38+
"eslint": "^8.57.0",
39+
"eslint-plugin-import": "^2.29.1",
40+
"eslint-plugin-isaacscript": "^3.12.2",
41+
"eslint-plugin-only-warn": "^1.1.0",
42+
"obsidian": "latest",
43+
"prettier": "^3.2.5",
44+
"prettier-plugin-svelte": "^3.2.2",
45+
"shiki": "^1.2.0",
46+
"string-argv": "^0.3.2",
47+
"svelte": "^4.2.12",
48+
"svelte-check": "^3.6.8",
49+
"svelte-preprocess": "^5.1.3",
50+
"tslib": "^2.6.2",
51+
"typescript": "^5.4.3"
52+
}
5153
}

src/main.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
import { Plugin } from 'obsidian';
22
import { bundledLanguages, getHighlighter } from 'shiki';
33
import { OBSIDIAN_THEME } from 'src/ObsidianTheme';
4+
import { ExpressiveCodeEngine, ExpressiveCodeTheme } from '@expressive-code/core';
5+
import { pluginShiki } from '@expressive-code/plugin-shiki';
6+
import { toHtml } from 'hast-util-to-html';
47

58
// some languages break obsidian's `registerMarkdownCodeBlockProcessor`, so we blacklist them
69
const languageNameBlacklist = new Set(['c++', 'c#', 'f#']);
710

811
export default class ShikiPlugin extends Plugin {
912
async onload(): Promise<void> {
10-
const highlighter = await getHighlighter({
11-
themes: [OBSIDIAN_THEME],
12-
langs: Object.keys(bundledLanguages),
13-
});
13+
// const highlighter = await getHighlighter({
14+
// themes: [OBSIDIAN_THEME],
15+
// langs: Object.keys(bundledLanguages),
16+
// });
1417

1518
const registeredLanguages = new Set<string>();
1619

20+
const ec = new ExpressiveCodeEngine({
21+
themes: [new ExpressiveCodeTheme(OBSIDIAN_THEME as any)],
22+
plugins: [
23+
pluginShiki({
24+
langs: Object.entries(bundledLanguages).map(([_, langFn]) => langFn),
25+
}),
26+
],
27+
});
28+
29+
console.log(ec);
30+
1731
for (const [shikiLanguage, registration] of Object.entries(bundledLanguages)) {
1832
// the last element of the array is seemingly the most recent version of the language
1933
const language = (await registration()).default.at(-1);
@@ -37,16 +51,26 @@ export default class ShikiPlugin extends Plugin {
3751
registeredLanguages.add(languageAlias);
3852

3953
// register the language with obsidian
40-
this.registerMarkdownCodeBlockProcessor(languageAlias, async (source, el) => {
54+
this.registerMarkdownCodeBlockProcessor(languageAlias, async (source, el, ctx) => {
4155
// yes, this is innerHTML, but we trust shiki
4256
// and this is how shiki recommends using it
4357
// https://shiki.style/guide/install#shorthands
4458
// this is also async, against what eslint thinks
4559
// eslint-disable-next-line @typescript-eslint/await-thenable
46-
el.innerHTML = await highlighter.codeToHtml(source, {
47-
lang: shikiLanguage,
48-
theme: 'obsidian-theme',
60+
// el.innerHTML = await highlighter.codeToHtml(source, {
61+
// lang: shikiLanguage,
62+
// theme: 'obsidian-theme',
63+
// meta: {
64+
// __raw: '',
65+
// },
66+
// });
67+
68+
const rederResult = await ec.render({
69+
code: source,
70+
language: language.name,
4971
});
72+
73+
el.innerHTML = toHtml(rederResult.renderedGroupAst);
5074
});
5175
}
5276
}

0 commit comments

Comments
 (0)