-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(code-block): server-side prerendered prism
- Loading branch information
1 parent
9010ba8
commit 73fc6a4
Showing
7 changed files
with
185 additions
and
69 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
"@rhds/elements": minor | ||
--- | ||
`<rh-code-block>`: syntax highlighting via prerendered prismjs code-blocks. Use | ||
the `highlighting="prism-prerendered"` attribute when rendering code blocks using | ||
server side prism, e.g. in a markdown fenced code block. | ||
|
||
```html | ||
<rh-code-block highlighting="prism-prerendered"> | ||
<pre class="language-css"><code class="language-css"><span class="token selector">a</span> <span class="token punctuation">{</span> | ||
<span class="token property">color</span><span class="token punctuation">:</span> <span class="token function">var</span><span class="token punctuation">(</span>--rh-color-interactive-primary-default<span class="token punctuation">)</span><span class="token punctuation">;</span> | ||
<span class="token punctuation">}</span></pre> | ||
</rh-code-block> | ||
``` |
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
75 changes: 75 additions & 0 deletions
75
elements/rh-code-block/demo/prerendered-prism-highlighting.html
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,75 @@ | ||
<rh-context-demo> | ||
<rh-code-block highlighting="prism-prerendered"> | ||
<pre class="language-html"><code class="language-html"><span class="token doctype"><span class="token punctuation"><!</span><span class="token doctype-tag">DOCTYPE</span> <span class="token name">html</span><span class="token punctuation">></span></span> | ||
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>html</span> <span class="token attr-name">lang</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>en<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> | ||
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>head</span><span class="token punctuation">></span></span> | ||
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>meta</span> <span class="token attr-name">charset</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>utf-8<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> | ||
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>meta</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>viewport<span class="token punctuation">"</span></span> <span class="token attr-name">content</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>width=device-width<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> | ||
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>title</span><span class="token punctuation">></span></span>Cards Galore!<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>title</span><span class="token punctuation">></span></span> | ||
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>head</span><span class="token punctuation">></span></span> | ||
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>body</span><span class="token punctuation">></span></span> | ||
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>main</span><span class="token punctuation">></span></span> | ||
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>rh-card</span><span class="token punctuation">></span></span> | ||
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>h2</span> <span class="token attr-name">slot</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>header<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Card<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>h2</span><span class="token punctuation">></span></span> | ||
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>p</span><span class="token punctuation">></span></span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. | ||
Nullam eleifend elit sed est egestas, a sollicitudin mauris | ||
tincidunt. Pellentesque vel dapibus risus. Nullam aliquam | ||
felis orci, eget cursus mi lacinia quis. Vivamus at felis sem.<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>p</span><span class="token punctuation">></span></span> | ||
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>rh-cta</span> <span class="token attr-name">slot</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>footer<span class="token punctuation">"</span></span> <span class="token attr-name">priority</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>primary<span class="token punctuation">"</span></span><span class="token punctuation">></span></span> | ||
<span class="token tag"><span class="token tag"><span class="token punctuation"><</span>a</span> <span class="token attr-name">href</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>#<span class="token punctuation">"</span></span><span class="token punctuation">></span></span>Call to action<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>a</span><span class="token punctuation">></span></span> | ||
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>rh-cta</span><span class="token punctuation">></span></span> | ||
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>rh-card</span><span class="token punctuation">></span></span> | ||
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>main</span><span class="token punctuation">></span></span> | ||
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>body</span><span class="token punctuation">></span></span> | ||
<span class="token tag"><span class="token tag"><span class="token punctuation"></</span>html</span><span class="token punctuation">></span></span></code></pre> | ||
</rh-code-block> | ||
|
||
<rh-code-block highlighting="prism-prerendered"> | ||
<pre class="language-css"><code class="language-css"><span class="token selector">rh-card.avatar-card</span> <span class="token punctuation">{</span> | ||
<span class="token property">width</span><span class="token punctuation">:</span> 360px<span class="token punctuation">;</span> | ||
<span class="token selector">&::part(body)</span> <span class="token punctuation">{</span> | ||
<span class="token property">margin-block-start</span><span class="token punctuation">:</span> <span class="token function">var</span><span class="token punctuation">(</span>--rh-space-lg<span class="token punctuation">,</span> 16px<span class="token punctuation">)</span><span class="token punctuation">;</span> | ||
<span class="token punctuation">}</span> | ||
|
||
<span class="token selector">& p</span> <span class="token punctuation">{</span> | ||
<span class="token property">margin-block-start</span><span class="token punctuation">:</span> 0<span class="token punctuation">;</span> | ||
<span class="token punctuation">}</span> | ||
|
||
<span class="token selector">& h4</span> <span class="token punctuation">{</span> | ||
<span class="token property">font-weight</span><span class="token punctuation">:</span> <span class="token function">var</span><span class="token punctuation">(</span>--rh-font-weight-heading-regular<span class="token punctuation">,</span> 300<span class="token punctuation">)</span><span class="token punctuation">;</span> | ||
<span class="token property">font-size</span><span class="token punctuation">:</span> <span class="token function">var</span><span class="token punctuation">(</span>--rh-font-size-body-text-md<span class="token punctuation">,</span> 1rem<span class="token punctuation">)</span><span class="token punctuation">;</span> | ||
<span class="token property">font-family</span><span class="token punctuation">:</span> <span class="token function">var</span><span class="token punctuation">(</span>--rh-font-family-body-text<span class="token punctuation">)</span><span class="token punctuation">;</span> | ||
<span class="token property">line-height</span><span class="token punctuation">:</span> <span class="token function">var</span><span class="token punctuation">(</span>--rh-line-height-body-text<span class="token punctuation">,</span> 1.5<span class="token punctuation">)</span><span class="token punctuation">;</span> | ||
<span class="token punctuation">}</span> | ||
<span class="token punctuation">}</span></code></pre> | ||
</rh-code-block> | ||
|
||
<rh-code-block highlighting="prism-prerendered"> | ||
<pre class="language-yaml"><code class="language-yaml"><span class="token key atrule">extends</span><span class="token punctuation">:</span> | ||
<span class="token punctuation">-</span> stylelint<span class="token punctuation">-</span>config<span class="token punctuation">-</span>standard | ||
<span class="token punctuation">-</span> <span class="token string">'@stylistic/stylelint-config'</span> | ||
|
||
<span class="token key atrule">plugins</span><span class="token punctuation">:</span> | ||
<span class="token punctuation">-</span> ./node_modules/@rhds/tokens/plugins/stylelint.js | ||
<span class="token punctuation">-</span> <span class="token string">'@stylistic/stylelint-plugin'</span> | ||
|
||
<span class="token key atrule">rules</span><span class="token punctuation">:</span> | ||
<span class="token key atrule">rhds/token-values</span><span class="token punctuation">:</span> <span class="token boolean important">true</span> | ||
<span class="token key atrule">rhds/no-unknown-token-name</span><span class="token punctuation">:</span> | ||
<span class="token punctuation">-</span> <span class="token boolean important">true</span> | ||
<span class="token punctuation">-</span> <span class="token key atrule">allowed</span><span class="token punctuation">:</span> | ||
<span class="token punctuation">-</span> <span class="token punctuation">-</span><span class="token punctuation">-</span>rh<span class="token punctuation">-</span>icon<span class="token punctuation">-</span>size</code></pre> | ||
</rh-code-block> | ||
</rh-context-demo> | ||
|
||
<script type="module"> | ||
import '@rhds/elements/lib/elements/rh-context-demo/rh-context-demo.js'; | ||
import '@rhds/elements/rh-code-block/rh-code-block.js'; | ||
</script> | ||
|
||
<style> | ||
rh-context-demo::part(demo) { | ||
display: grid; | ||
gap: var(--rh-space-sm, 6px); | ||
} | ||
</style> |
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 |
---|---|---|
@@ -1,73 +1,82 @@ | ||
import { css } from 'lit'; | ||
|
||
export const prismStyles = css` code[class*="language-"], | ||
pre[class*="language-"] { | ||
color: var(--_code-color); | ||
font-family: var(--rh-font-family-code, RedHatMono, "Red Hat Mono", "Courier New", Courier, monospace); | ||
text-align: left; | ||
white-space: pre; | ||
word-spacing: normal; | ||
word-break: normal; | ||
word-wrap: normal; | ||
line-height: var(--rh-line-height-code, 1.5); | ||
tab-size: 4; | ||
hyphens: none; | ||
} | ||
pre[class*="language-"]::selection, | ||
pre[class*="language-"] ::selection, | ||
code[class*="language-"]::selection, | ||
code[class*="language-"] ::selection { | ||
text-shadow: none; | ||
background: var(--_selected-text-background); | ||
} | ||
const styles = css` | ||
& code[class*="language-"], | ||
& pre[class*="language-"] { | ||
color: var(--_code-color); | ||
font-family: var(--rh-font-family-code, RedHatMono, "Red Hat Mono", "Courier New", Courier, monospace); | ||
text-align: left; | ||
white-space: pre; | ||
word-spacing: normal; | ||
word-break: normal; | ||
word-wrap: normal; | ||
line-height: var(--rh-line-height-code, 1.5); | ||
tab-size: 4; | ||
hyphens: none; | ||
background: transparent; | ||
} | ||
@media print { | ||
code[class*="language-"], | ||
pre[class*="language-"] { | ||
& pre[class*="language-"]::selection, | ||
& pre[class*="language-"] ::selection, | ||
& code[class*="language-"]::selection, | ||
& code[class*="language-"] ::selection { | ||
text-shadow: none; | ||
background: var(--_selected-text-background); | ||
} | ||
@media print { | ||
& code[class*="language-"], | ||
& pre[class*="language-"] { | ||
text-shadow: none; | ||
} | ||
} | ||
} | ||
.token.atrule { color: var(--_at-rule-color); } | ||
.token.attr-name { color: var(--_attr-name-color); } | ||
.token.attr-value { color: var(--_attr-value-color); } | ||
.token.bold { font-weight: var(--_important-color); } | ||
.token.boolean { color: var(--_boolean-color); } | ||
.token.builtin { color: var(--_built-in-color); } | ||
.token.cdata { color: var(--_cdata-color); } | ||
.token.char { color: var(--_character-color); } | ||
.token.class-name { color: var(--_class-name-color); } | ||
.token.comment { color: var(--_comment-color); } | ||
.token.constant { color: var(--_constant-color); } | ||
.token.deleted { color: var(--_deleted-color); } | ||
.token.function { color: var(--_function-name-color); } | ||
.token.important { color: var(--_important-color); } | ||
.token.inserted { color: var(--_inserted-color); } | ||
.token.keyword { color: var(--_keyword-color); } | ||
.token.namespace { color: var(--_namespace-color); } | ||
.token.number { color: var(--_number-color); } | ||
.token.operator { color: var(--_operator-color); } | ||
.token.property { color: var(--_property-color); } | ||
.token.punctuation { color: var(--_punctuation-color); } | ||
.token.regex { color: var(--_regex-color); } | ||
.token.selector { color: var(--_selector-color); } | ||
.token.string { color: var(--_string-color); } | ||
.token.symbol { color: var(--_symbol-color); } | ||
.token.tag { color: var(--_tag-color); } | ||
.token.url { color: var(--_url-color); } | ||
.token.variable { color: var(--_variable-color); } | ||
& .token.atrule { color: var(--_at-rule-color); } | ||
& .token.attr-name { color: var(--_attr-name-color); } | ||
& .token.attr-value { color: var(--_attr-value-color); } | ||
& .token.bold { font-weight: var(--_important-color); } | ||
& .token.boolean { color: var(--_boolean-color); } | ||
& .token.builtin { color: var(--_built-in-color); } | ||
& .token.cdata { color: var(--_cdata-color); } | ||
& .token.char { color: var(--_character-color); } | ||
& .token.class-name { color: var(--_class-name-color); } | ||
& .token.comment { color: var(--_comment-color); } | ||
& .token.constant { color: var(--_constant-color); } | ||
& .token.deleted { color: var(--_deleted-color); } | ||
& .token.function { color: var(--_function-name-color); } | ||
& .token.important { color: var(--_important-color); } | ||
& .token.inserted { color: var(--_inserted-color); } | ||
& .token.keyword { color: var(--_keyword-color); } | ||
& .token.namespace { color: var(--_namespace-color); } | ||
& .token.number { color: var(--_number-color); } | ||
& .token.operator { color: var(--_operator-color); } | ||
& .token.property { color: var(--_property-color); } | ||
& .token.punctuation { color: var(--_punctuation-color); } | ||
& .token.regex { color: var(--_regex-color); } | ||
& .token.selector { color: var(--_selector-color); } | ||
& .token.string { color: var(--_string-color); } | ||
& .token.symbol { color: var(--_symbol-color); } | ||
& .token.tag { color: var(--_tag-color); } | ||
& .token.url { color: var(--_url-color); } | ||
& .token.variable { color: var(--_variable-color); } | ||
.token.italic { font-style: italic; } | ||
& .token.italic { font-style: italic; } | ||
.token.entity { | ||
color: var(--_entity-color); | ||
cursor: help; | ||
} | ||
& .token.entity { | ||
color: var(--_entity-color); | ||
cursor: help; | ||
} | ||
.token.prolog, | ||
.token.doctype { color: var(--_doctype-color); } | ||
& .token.prolog, | ||
& .token.doctype { color: var(--_doctype-color); } | ||
.language-css .token.string, | ||
.style .token.string { color: var(--_operator-color); } | ||
& .language-css .token.string, | ||
& .style .token.string { color: var(--_operator-color); } | ||
`; | ||
|
||
export const prismStyles = css`#prism-output {${styles}}`; | ||
export const preRenderedLightDomStyles = css`rh-code-block { | ||
--_styles-applied: true; | ||
${styles} | ||
& > pre { opacity: 1; } | ||
}`; |
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