-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
APP-7173: Add Prism code highlighting action (#605)
Co-authored-by: emily hong <[email protected]>
- Loading branch information
Showing
11 changed files
with
91 additions
and
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import type { Action } from 'svelte/action'; | ||
import { getPrismModule } from './prism'; | ||
|
||
/** | ||
* Highlight code in any child `<pre><code /></pre>` blocks. | ||
* Elements should be styled according to Prism's expected HTML structure. | ||
* highlighting only runs on mount of the element, so the element using the action should be a keyed element to force re-highlighting on updates. | ||
* | ||
* Usage example: | ||
* ```svelte | ||
* <script> | ||
* import { highlightCode } from '@viamrobotics/prime-core'; | ||
* export let code: string; | ||
* </script> | ||
* | ||
* {#key code} | ||
* <div use:highlightCode> | ||
* <pre><code class="language-javascript">{code}</code></pre> | ||
* </div> | ||
* {/key} | ||
* ``` | ||
* | ||
* @param node - The node to highlight. | ||
*/ | ||
export const highlightCode: Action<HTMLElement | undefined> = ( | ||
node: HTMLElement | undefined | ||
) => { | ||
if (node) { | ||
highlightContainerElement(node); | ||
} | ||
}; | ||
|
||
const highlightContainerElement = (element: Element) => { | ||
const prism = getPrismModule(); | ||
prism.highlightAllUnder(element); | ||
}; |
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 @@ | ||
export { highlightCode } from './highlight-code'; |
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,25 @@ | ||
/** | ||
* Wrapper module for Prism.js | ||
*/ | ||
|
||
import PrismPackage from 'prismjs/package.json'; | ||
import Prism from 'prismjs'; | ||
import './viam-prism-theme.css'; | ||
|
||
/** | ||
* We use the prism autoloader to handle loading in language grammar files. The | ||
* default path for the grammar files is a CDN link so we don't have to include | ||
* grammar files in our bundle. If you prefer to point to your own grammars and | ||
* bypass the CDN, define the path to those files. | ||
* | ||
* See: https://prismjs.com/plugins/autoloader/ | ||
*/ | ||
import 'prismjs/plugins/autoloader/prism-autoloader'; | ||
const grammarsPath = `https://cdnjs.cloudflare.com/ajax/libs/prism/${PrismPackage.version}/components/`; | ||
|
||
export const getPrismModule = (): typeof Prism => { | ||
// Make sure the autoloader knows where to find our languages | ||
(Prism.plugins.autoloader as { languages_path: string }).languages_path = | ||
grammarsPath; | ||
return Prism; | ||
}; |
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,17 @@ | ||
@import 'prism-themes/themes/prism-vs.min.css'; | ||
|
||
pre[class*='language-'], | ||
pre[class*='language-'] > code[class*='language-'] { | ||
margin: 0; | ||
border: none; | ||
background: inherit; | ||
} | ||
|
||
pre[class*='language-'] { | ||
padding: 0; | ||
} | ||
|
||
pre[class*='language-'] > code[class*='language-'] { | ||
font-family: 'Roboto Mono Variable', 'Roboto Mono', ui-monospace, monospace; | ||
font-size: 1em; | ||
} |
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