-
-
Notifications
You must be signed in to change notification settings - Fork 262
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add syntax highlighting for component API
- Loading branch information
Showing
4 changed files
with
154 additions
and
20 deletions.
There are no files selected for viewing
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
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,60 @@ | ||
<script context="module"> | ||
// Lazy load dependencies for performance. | ||
const asyncFormat = Promise.all([ | ||
import("prettier/standalone"), | ||
import("prettier/parser-typescript"), | ||
]).then(([prettier, typescriptParser]) => prettier.format); | ||
const asyncHighlight = import("prismjs").then((module) => { | ||
import("prismjs/components/prism-typescript"); | ||
return module.highlight; | ||
}); | ||
</script> | ||
|
||
<script> | ||
export let code = ""; | ||
export let noFormat = false; | ||
export let type = "multi"; | ||
import { CodeSnippet } from "carbon-components-svelte"; | ||
import copy from "clipboard-copy"; | ||
let formattedCode = ""; | ||
let highlightedCode = ""; | ||
$: { | ||
asyncFormat.then((format) => { | ||
try { | ||
formattedCode = noFormat | ||
? code | ||
: format(code, { parser: "typescript", plugins: [typescriptParser] }); | ||
} catch (e) { | ||
formattedCode = code; | ||
} | ||
}); | ||
} | ||
$: { | ||
asyncHighlight.then((highlight) => { | ||
highlightedCode = highlight( | ||
formattedCode, | ||
Prism.languages.typescript, | ||
"typescript", | ||
); | ||
}); | ||
} | ||
</script> | ||
|
||
{#if type === "multi"} | ||
<div class="code-override"> | ||
<CodeSnippet type="multi" code={formattedCode} {copy}> | ||
{@html highlightedCode} | ||
</CodeSnippet> | ||
</div> | ||
{/if} | ||
|
||
{#if type === "inline"} | ||
<CodeSnippet type="inline" code={formattedCode} {copy}> | ||
{@html highlightedCode} | ||
</CodeSnippet> | ||
{/if} |
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