-
-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(shortcuts): guess embedded language and auto load in shortcuts (#…
…932)
- Loading branch information
Showing
13 changed files
with
198 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { HighlighterGeneric } from '@shikijs/types' | ||
|
||
export function guessEmbeddedLanguages( | ||
code: string, | ||
_lang: string | undefined, | ||
shiki: HighlighterGeneric<any, any>, | ||
): string[] { | ||
const langs = new Set<string>() | ||
// For HTML code blocks like Vue SFC | ||
for (const match of code.matchAll(/lang=["']([\w-]+)["']/g)) { | ||
langs.add(match[1]) | ||
} | ||
// For markdown code blocks | ||
for (const match of code.matchAll(/(?:```|~~~)([\w-]+)/g)) { | ||
langs.add(match[1]) | ||
} | ||
// For latex | ||
for (const match of code.matchAll(/\\begin\{([\w-]+)\}/g)) { | ||
langs.add(match[1]) | ||
} | ||
|
||
// Only include known languages | ||
const bundle = shiki.getBundledLanguages() | ||
return Array.from(langs) | ||
.filter(l => l && bundle[l]) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { expect, it } from 'vitest' | ||
import { codeToHtml, getSingletonHighlighter } from '../src' | ||
|
||
const inputMarkdown1 = ` | ||
This is a markdown file | ||
\`\`\`js | ||
console.log("hello") | ||
\`\`\` | ||
~~~pug | ||
div | ||
p hello | ||
~~~ | ||
Even those grammars in markdown are lazy loaded, \`codeToHtml\` shorthand should capture them and load automatically. | ||
` | ||
|
||
const inputMarkdown2 = ` | ||
Some other languages | ||
\`\`\`js | ||
console.log("hello") | ||
\`\`\` | ||
~~~python | ||
print("hello") | ||
~~~ | ||
\`\`\`html | ||
<div class="foo">bar</div> | ||
<style> | ||
.foo { | ||
color: red; | ||
} | ||
</style> | ||
\`\`\` | ||
` | ||
|
||
it('codeToHtml', async () => { | ||
const highlighter = await getSingletonHighlighter() | ||
expect(highlighter.getLoadedLanguages()) | ||
.toEqual([]) | ||
|
||
await expect(await codeToHtml(inputMarkdown1, { lang: 'markdown', theme: 'vitesse-light' })) | ||
.toMatchFileSnapshot(`out/shorthand-markdown1.html`) | ||
|
||
expect.soft(highlighter.getLoadedLanguages()) | ||
.toContain('javascript') | ||
expect.soft(highlighter.getLoadedLanguages()) | ||
.toContain('pug') | ||
|
||
await expect(await codeToHtml(inputMarkdown2, { lang: 'markdown', theme: 'vitesse-light' })) | ||
.toMatchFileSnapshot(`out/shorthand-markdown2.html`) | ||
|
||
expect.soft(highlighter.getLoadedLanguages()) | ||
.toContain('python') | ||
expect.soft(highlighter.getLoadedLanguages()) | ||
.toContain('html') | ||
|
||
expect.soft(highlighter.getLoadedLanguages()) | ||
.toMatchInlineSnapshot(` | ||
[ | ||
"javascript", | ||
"css", | ||
"html", | ||
"pug", | ||
"python", | ||
"markdown", | ||
"md", | ||
"js", | ||
"jade", | ||
"py", | ||
] | ||
`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.