Skip to content

Commit 08da02c

Browse files
committed
Support GFM and syntax highlighting in Volar example
1 parent 6d222f4 commit 08da02c

File tree

4 files changed

+134
-10
lines changed

4 files changed

+134
-10
lines changed

example/scripts/json.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ import {
1414
textDocument
1515
} from 'codemirror-languageservice'
1616
import { toDom } from 'hast-util-to-dom'
17-
import { fromMarkdown } from 'mdast-util-from-markdown'
18-
import { toHast } from 'mdast-util-to-hast'
17+
import rehypeStarryNight from 'rehype-starry-night'
18+
import remarkParse from 'remark-parse'
19+
import remarkRehype from 'remark-rehype'
20+
import { unified } from 'unified'
1921
import {
2022
getLanguageService,
2123
type JSONDocument,
@@ -24,6 +26,8 @@ import {
2426

2527
import pkg from '../../package.json'
2628

29+
const processor = unified().use(remarkParse).use(remarkRehype).use(rehypeStarryNight)
30+
2731
/**
2832
* Convert markdown content to a DOM node.
2933
*
@@ -32,9 +36,9 @@ import pkg from '../../package.json'
3236
* @returns
3337
* The DOM node that represents the markdown.
3438
*/
35-
function markdownToDom(markdown: string): Node {
36-
const mdast = fromMarkdown(markdown)
37-
const hast = toHast(mdast)
39+
async function markdownToDom(markdown: string): Promise<Node> {
40+
const mdast = processor.parse(markdown)
41+
const hast = await processor.run(mdast)
3842
const html = toDom(hast, { fragment: true })
3943
return html
4044
}

example/scripts/typescript.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import '@wooorm/starry-night/style/both'
2+
13
import { autocompletion, closeBracketsKeymap, completionKeymap } from '@codemirror/autocomplete'
24
import { defaultKeymap, history, historyKeymap } from '@codemirror/commands'
35
import { javascript } from '@codemirror/lang-javascript'
@@ -15,6 +17,7 @@ import {
1517
type SourceScript
1618
} from '@volar/language-service'
1719
import { createLanguageServiceHost, createSys, resolveFileLanguageId } from '@volar/typescript'
20+
import { all } from '@wooorm/starry-night'
1821
import {
1922
createCompletionSource,
2023
createHoverTooltipSource,
@@ -24,9 +27,12 @@ import {
2427
textDocument
2528
} from 'codemirror-languageservice'
2629
import { toDom } from 'hast-util-to-dom'
27-
import { fromMarkdown } from 'mdast-util-from-markdown'
28-
import { toHast } from 'mdast-util-to-hast'
30+
import rehypeStarryNight from 'rehype-starry-night'
31+
import remarkGfm from 'remark-gfm'
32+
import remarkParse from 'remark-parse'
33+
import remarkRehype from 'remark-rehype'
2934
import * as ts from 'typescript'
35+
import { unified } from 'unified'
3036
import { create as createTypeScriptPlugins } from 'volar-service-typescript'
3137
import { type TextDocument } from 'vscode-languageserver-textdocument'
3238
import { URI } from 'vscode-uri'
@@ -37,6 +43,12 @@ globalThis.process = {
3743
}
3844
} as NodeJS.Process
3945

46+
const processor = unified()
47+
.use(remarkParse)
48+
.use(remarkGfm)
49+
.use(remarkRehype)
50+
.use(rehypeStarryNight, { grammars: all })
51+
4052
/**
4153
* Convert markdown content to a DOM node.
4254
*
@@ -45,9 +57,9 @@ globalThis.process = {
4557
* @returns
4658
* The DOM node that represents the markdown.
4759
*/
48-
function markdownToDom(markdown: string): Node {
49-
const mdast = fromMarkdown(markdown)
50-
const hast = toHast(mdast)
60+
async function markdownToDom(markdown: string): Promise<Node> {
61+
const mdast = processor.parse(markdown)
62+
const hast = await processor.run(mdast)
5163
const html = toDom(hast, { fragment: true })
5264
return html
5365
}

package-lock.json

Lines changed: 102 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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,20 @@
5454
"@volar/jsdelivr": "~2.4.0",
5555
"@volar/language-service": "~2.4.0",
5656
"@volar/typescript": "~2.4.0",
57+
"@wooorm/starry-night": "^3.0.0",
5758
"hast-util-to-dom": "^4.0.0",
5859
"mdast-util-from-markdown": "^2.0.0",
5960
"mdast-util-to-hast": "^13.0.0",
6061
"playwright": "^1.0.0",
6162
"prettier": "^3.0.0",
63+
"rehype-starry-night": "^2.0.0",
6264
"remark-cli": "^12.0.0",
65+
"remark-gfm": "^4.0.0",
66+
"remark-parse": "^11.0.0",
6367
"remark-preset-remcohaszing": "^3.0.0",
68+
"remark-rehype": "^11.0.0",
6469
"typescript": "^5.0.0",
70+
"unified": "^11.0.0",
6571
"vite": "^6.0.0",
6672
"vitest": "^3.0.0",
6773
"volar-service-typescript": "0.0.64",

0 commit comments

Comments
 (0)