-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathrust.stories.js
126 lines (112 loc) · 4.07 KB
/
rust.stories.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { storiesOf } from '@storybook/html';
import copyCodeBlock from '../src/copyCodeBlock';
import { usageExample, usageExampleJsHighlight } from './helpers';
import { cssOverrides } from './customHtml';
import hljs from 'highlight.js/lib/highlight';
// Register languages for hljs
hljs.registerLanguage('rust', require('highlight.js/lib/languages/rust'));
hljs.registerLanguage('javascript', require('highlight.js/lib/languages/javascript'));
const a11yLightStyle = require('!url-loader!highlight.js/styles/a11y-light.css');
const draculaStyle = require('!url-loader!highlight.js/styles/dracula.css');
const rustExample = `
#![meta "meta-string"]
fn main() {
// The statements here will be executed when the compiled binary is called
// Print text to the console
println!("Hello World!");
}
`.trim();
const hljsOpts = {
lang: 'rust',
colors: {
textColor: '#222',
function: '#266',
title: '#a42',
comment: '#2a2',
built_in: '#a26',
string: '#288',
meta: '#555',
metaString: '#d2d' /* camelCase is converted to hyphen-case */
}
};
const noHljsOpts = {
colors: {
background: '#111',
textColor: '#efa'
}
}
storiesOf('Rust', module)
.add('Code File', () => `
<link rel="stylesheet" href="${draculaStyle}">
<h1>Code File</h1>
<h2>Example Code</h2>
${copyCodeBlock(rustExample)}
<h2>Usage</h2>
${copyCodeBlock(usageExample(null, 'rs'), usageExampleJsHighlight)}
`)
.add('Code String', () => `
<link rel="stylesheet" href="${draculaStyle}">
<h1>Code String</h1>
<h2>Example Code</h2>
${copyCodeBlock(rustExample)}
<h2>Usage</h2>
${copyCodeBlock(usageExample(), usageExampleJsHighlight)}
`)
.add('Custom styles, no syntax highlighting', () => `
<link rel="stylesheet" href="${draculaStyle}">
<h1>Custom styles, no syntax highlighting</h1>
<h2>Example Code</h2>
${copyCodeBlock(rustExample, noHljsOpts)}
<h2>Usage</h2>
${copyCodeBlock(usageExample(noHljsOpts), usageExampleJsHighlight)}
`)
.add('Syntax highlighting, no custom styles', () => `
<link rel="stylesheet" href="${a11yLightStyle}">
<h1>Syntax highlighting, no custom styles</h1>
<p>
This example uses
<a
href="https://github.com/highlightjs/highlight.js/blob/master/src/styles/a11y-light.css"
target="_blank"
>a11y-light.css</a>
from highlight.js, by adding it to the HTML's stylesheets.
</p>
<h2>Example Code</h2>
${copyCodeBlock(rustExample, {lang: 'rust'})}
<h2>Usage</h2>
${copyCodeBlock(usageExample({lang: 'rust'}), usageExampleJsHighlight)}
`)
.add('Syntax highlighting & custom styles', () => `
<link rel="stylesheet" href="${draculaStyle}">
<h1>Syntax highlighting, no custom styles</h1>
<h2>Example Code</h2>
${copyCodeBlock(rustExample, hljsOpts)}
<h2>Usage</h2>
${copyCodeBlock(usageExample(hljsOpts), usageExampleJsHighlight)}
`)
.add('CSS overrides for copy button', () => `
<link rel="stylesheet" href="${draculaStyle}">
<h1>Syntax highlighting, css overrides</h1>
<h2>Example Code</h2>
${copyCodeBlock(rustExample, cssOverrides)}
<h2>Usage</h2>
${copyCodeBlock(usageExample(cssOverrides), usageExampleJsHighlight)}
`)
.add('Return DOM element', () => {
const options = {shouldReturnDomEl: true};
const container = document.createElement('div');
const link = document.createElement('link');
link.rel = 'stylesheet';
link.href = draculaStyle;
const titleHeader = document.createElement('h1');
titleHeader.innerHTML = 'Return DOM element';
const exampleHeader = document.createElement('h2');
exampleHeader.innerHTML = 'Example Code';
const domEl = copyCodeBlock(rustExample, options);
const usageHeader = document.createElement('h2');
usageHeader.innerHTML = 'Usage Code';
const jsUsageExample = document.createElement('div');
jsUsageExample.innerHTML = copyCodeBlock(usageExample(options), usageExampleJsHighlight);
container.append(link, titleHeader, exampleHeader, domEl, usageHeader, jsUsageExample);
return container;
});