Skip to content

Commit dea9867

Browse files
committed
feat: add docs-code component
1 parent 4195c94 commit dea9867

File tree

8 files changed

+153
-2
lines changed

8 files changed

+153
-2
lines changed

package-lock.json

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"fast-glob": "^2.2.4",
2121
"front-matter": "^3.0.1",
2222
"fs-extra": "^7.0.1",
23+
"highlight.js": "^9.13.1",
2324
"jsdom": "^13.0.0",
2425
"listr": "^0.14.3",
2526
"marked": "^0.5.2",
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
import marked from 'marked';
2-
export default (markdownString) => marked(markdownString);
2+
import hljs from 'highlight.js';
3+
4+
const renderer = new marked.Renderer();
5+
6+
renderer.code = (code: string, lang = '') =>
7+
`<docs-code language="${lang}">${hljs.highlightAuto(code, [lang]).value}</docs-code>`;
8+
9+
export default (markdownString) => marked(markdownString, { renderer });

src/components.d.ts

+17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ import '@stencil/state-tunnel';
1313

1414
export namespace Components {
1515

16+
interface DocsCode {
17+
'language': string;
18+
}
19+
interface DocsCodeAttributes extends StencilHTMLAttributes {
20+
'language'?: string;
21+
}
22+
1623
interface DocsDocument {
1724
'path': string;
1825
}
@@ -29,18 +36,26 @@ export namespace Components {
2936

3037
declare global {
3138
interface StencilElementInterfaces {
39+
'DocsCode': Components.DocsCode;
3240
'DocsDocument': Components.DocsDocument;
3341
'DocsMenu': Components.DocsMenu;
3442
'DocsRoot': Components.DocsRoot;
3543
}
3644

3745
interface StencilIntrinsicElements {
46+
'docs-code': Components.DocsCodeAttributes;
3847
'docs-document': Components.DocsDocumentAttributes;
3948
'docs-menu': Components.DocsMenuAttributes;
4049
'docs-root': Components.DocsRootAttributes;
4150
}
4251

4352

53+
interface HTMLDocsCodeElement extends Components.DocsCode, HTMLStencilElement {}
54+
var HTMLDocsCodeElement: {
55+
prototype: HTMLDocsCodeElement;
56+
new (): HTMLDocsCodeElement;
57+
};
58+
4459
interface HTMLDocsDocumentElement extends Components.DocsDocument, HTMLStencilElement {}
4560
var HTMLDocsDocumentElement: {
4661
prototype: HTMLDocsDocumentElement;
@@ -60,12 +75,14 @@ declare global {
6075
};
6176

6277
interface HTMLElementTagNameMap {
78+
'docs-code': HTMLDocsCodeElement
6379
'docs-document': HTMLDocsDocumentElement
6480
'docs-menu': HTMLDocsMenuElement
6581
'docs-root': HTMLDocsRootElement
6682
}
6783

6884
interface ElementTagNameMap {
85+
'docs-code': HTMLDocsCodeElement;
6986
'docs-document': HTMLDocsDocumentElement;
7087
'docs-menu': HTMLDocsMenuElement;
7188
'docs-root': HTMLDocsRootElement;

src/components/code/code.css

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
@import './highlight-theme.css';
2+
3+
docs-code {
4+
display: block;
5+
}
6+
7+
docs-code pre {
8+
position: relative;
9+
}
10+
11+
docs-code pre:before {
12+
color: #bdc5d1;
13+
content: attr(data-language);
14+
font-family: var(--code-font-family);
15+
font-size: 11px;
16+
letter-spacing: 0.05em;
17+
padding: 0.65em;
18+
position: absolute;
19+
right: 0;
20+
text-transform: uppercase;
21+
top: 0;
22+
}

src/components/code/code.tsx

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Component, Prop } from '@stencil/core';
2+
3+
@Component({
4+
tag: 'docs-code',
5+
styleUrl: 'code.css'
6+
})
7+
export class DocsCode {
8+
@Prop() language = '';
9+
10+
render() {
11+
return (
12+
<pre data-language={this.language}><code><slot/></code></pre>
13+
);
14+
}
15+
}
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
2+
3+
/* Tomorrow Comment */
4+
.hljs-comment,
5+
.hljs-quote {
6+
color: #8c9296;
7+
}
8+
9+
/* Tomorrow Red */
10+
.hljs-variable,
11+
.hljs-template-variable,
12+
.hljs-name,
13+
.hljs-selector-id,
14+
.hljs-selector-class,
15+
.hljs-regexp,
16+
.hljs-deletion {
17+
color: #2b90ff;
18+
}
19+
20+
.hljs-tag {
21+
color: #91c5ff;
22+
}
23+
24+
.hljs-tag .hljs-name {
25+
color: #2b90ff;
26+
}
27+
28+
/* Tomorrow Orange */
29+
.hljs-number,
30+
.hljs-built_in,
31+
.hljs-builtin-name,
32+
.hljs-literal,
33+
.hljs-type,
34+
.hljs-params,
35+
.hljs-meta,
36+
.hljs-link {
37+
color: #ff6810;
38+
}
39+
40+
/* Tomorrow Yellow */
41+
.hljs-attribute,
42+
.hljs-attr {
43+
color: #8454ff;
44+
}
45+
46+
/* Tomorrow Green */
47+
.hljs-string,
48+
.hljs-symbol,
49+
.hljs-bullet,
50+
.hljs-addition,
51+
.hljs-string a {
52+
color: #42b983;
53+
}
54+
55+
/* Tomorrow Blue */
56+
.hljs-title,
57+
.hljs-section {
58+
color: #ffbb01;
59+
}
60+
61+
/* Tomorrow Purple */
62+
.hljs-keyword,
63+
.hljs-selector-tag {
64+
color: #f55073;
65+
}
66+
67+
.hljs {
68+
display: block;
69+
overflow-x: auto;
70+
background: white;
71+
color: #4d4d4c;
72+
padding: 0.5em;
73+
}
74+
75+
.hljs-emphasis {
76+
font-style: italic;
77+
}
78+
79+
.hljs-strong {
80+
font-weight: bold;
81+
}

src/components/document/document.css

+3-1
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,16 @@ pre {
102102
border-radius: 4px;
103103
line-height: 1.2;
104104
margin: 2rem 0;
105-
overflow-x: auto;
105+
overflow: auto;
106106
padding: 20px;
107107
}
108108

109109
pre code {
110110
background-color: transparent;
111111
color: inherit;
112112
font-weight: 500;
113+
padding: 0;
114+
white-space: inherit;
113115
}
114116

115117
table {

0 commit comments

Comments
 (0)