Skip to content

Commit 76f5a70

Browse files
committed
feat(site) Add Prism color theme
1 parent 49a3e66 commit 76f5a70

8 files changed

+73
-18
lines changed

DISCUSSION.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ this branch:
1919
- [x] Add custom route for landing page (greg)
2020
- [x] Fixed sidebar (Fernando)
2121
- [x] Add anchor icons (Fernando)
22+
- [x] Add color theme (Fernando)
23+
- [ ] Create remark plugins for custom languages: `js-with-link`, `js-with-link-with-details` (Fernando)
24+
- [ ] Normalize all code tags: javascript -> js, sh|shell -> bash (Fernando)
2225

2326
Some of these should be fairly quick adds now that the site works. The two
2427
toughest ones are most likely the markdown parsing and external population. The

highlight.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const refractor = require('refractor/core.js');
2+
const visit = require('unist-util-visit');
3+
const languages = require('prism-languages');
4+
5+
refractor.register(require('refractor/lang/bash.js'));
6+
refractor.register(require('refractor/lang/diff.js'));
7+
refractor.register(require('refractor/lang/yaml.js'));
8+
refractor.register(require('refractor/lang/json.js'));
9+
refractor.register(require('refractor/lang/typescript.js'));
10+
refractor.register(require('refractor/lang/nginx.js'));
11+
refractor.register(require('refractor/lang/ruby.js'));
12+
13+
module.exports = function attacher({ include, exclude } = {}) {
14+
function visitor(node) {
15+
const { lang } = node;
16+
17+
if (
18+
!lang ||
19+
(include && !~include.indexOf(lang)) ||
20+
(exclude && ~exclude.indexOf(lang))
21+
) {
22+
return;
23+
}
24+
25+
let { data } = node;
26+
27+
if (!data) {
28+
node.data = data = {};
29+
}
30+
31+
try {
32+
data.hChildren = refractor.highlight(node.value, lang);
33+
} catch (e) {
34+
if (!languages[lang]) {
35+
console.warn('Prism does not support this language: ', lang);
36+
} else {
37+
console.warn('Prism failed to highlight: ', e);
38+
}
39+
}
40+
41+
data.hProperties = data.hProperties || {};
42+
data.hProperties.className = [
43+
'hljs',
44+
...(data.hProperties.className || []),
45+
`language-${lang}`
46+
];
47+
}
48+
49+
return ast => visit(ast, 'code', visitor);
50+
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"prism-languages": "^0.3.3",
8585
"prismjs": "^1.9.0",
8686
"raw-loader": "^0.5.1",
87+
"refractor": "2.3.0",
8788
"remark-autolink-headings": "^5.0.0",
8889
"remark-loader": "^0.3.0",
8990
"remark-mermaid": "^0.2.0",

src/content/api/node.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ webpack({
266266

267267
By default, webpack reads files and writes files to disk using a normal file system. However, it is possible to change the input or output behavior using a different kind of file system (memory, webDAV, etc). To accomplish this, one can change the `inputFileSystem` or `outputFileSystem`. For example, you can replace the default `outputFileSystem` with [`memory-fs`](https://github.com/webpack/memory-fs) to write files to memory instead of to disk:
268268

269-
``` js
269+
```js
270270
const MemoryFS = require("memory-fs");
271271
const webpack = require("webpack");
272272

src/content/contribute/release-process.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ When merging pull requests into the `master` branch, select the _Create Merge Co
1616

1717
## Releasing
1818

19-
```sh
19+
```bash
2020
npm version patch && git push --follow-tags && npm publish
2121
npm version minor && git push --follow-tags && npm publish
2222
npm version major && git push --follow-tags && npm publish

src/content/contribute/writing-a-loader.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ To test a single loader, you can simply use `path` to `resolve` a local file wit
1717

1818
__webpack.config.js__
1919

20-
``` js
20+
```js
2121
{
2222
test: /\.js$/
2323
use: [
@@ -33,7 +33,7 @@ To test multiple, you can utilize the `resolveLoader.modules` configuration to u
3333

3434
__webpack.config.js__
3535

36-
``` js
36+
```js
3737
resolveLoader: {
3838
modules: [
3939
'node_modules',
@@ -66,7 +66,7 @@ So, in the following example, the `foo-loader` would be passed the raw resource
6666

6767
__webpack.config.js__
6868

69-
``` js
69+
```js
7070
{
7171
test: /\.js/,
7272
use: [
@@ -122,7 +122,7 @@ Take advantage of the [`loader-utils`](https://github.com/webpack/loader-utils)
122122

123123
__loader.js__
124124

125-
``` js
125+
```js
126126
import { getOptions } from 'loader-utils';
127127
import validateOptions from 'schema-utils';
128128

@@ -152,7 +152,7 @@ If a loader uses external resources (i.e. by reading from filesystem), they __mu
152152

153153
__loader.js__
154154

155-
``` js
155+
```js
156156
import path from 'path';
157157

158158
export default function(source) {
@@ -197,7 +197,7 @@ If the loader you're working on is a simple wrapper around another package, then
197197

198198
For instance, the `sass-loader` [specifies `node-sass`](https://github.com/webpack-contrib/sass-loader/blob/master/package.json) as peer dependency like so:
199199

200-
``` js
200+
```js
201201
"peerDependencies": {
202202
"node-sass": "^4.0.0"
203203
}
@@ -208,13 +208,13 @@ For instance, the `sass-loader` [specifies `node-sass`](https://github.com/webpa
208208

209209
So you've written a loader, followed the guidelines above, and have it set up to run locally. What's next? Let's go through a simple unit testing example to ensure our loader is working the way we expect. We'll be using the [Jest](https://facebook.github.io/jest/) framework to do this. We'll also install `babel-jest` and some presets that will allow us to use the `import` / `export` and `async` / `await`. Let's start by installing and saving these as a `devDependencies`:
210210

211-
``` bash
211+
```bash
212212
npm install --save-dev jest babel-jest babel-preset-env
213213
```
214214

215215
__.babelrc__
216216

217-
``` json
217+
```json
218218
{
219219
"presets": [[
220220
"env",
@@ -231,7 +231,7 @@ Our loader will process `.txt` files and simply replace any instance of `[name]`
231231

232232
__src/loader.js__
233233

234-
``` js
234+
```js
235235
import { getOptions } from 'loader-utils';
236236

237237
export default function loader(source) {
@@ -247,19 +247,19 @@ We'll use this loader to process the following file:
247247

248248
__test/example.txt__
249249

250-
``` text
250+
```text
251251
Hey [name]!
252252
```
253253

254254
Pay close attention to this next step as we'll be using the [Node.js API](/api/node) and [`memory-fs`](https://github.com/webpack/memory-fs) to execute webpack. This lets us avoid emitting `output` to disk and will give us access to the `stats` data which we can use to grab our transformed module:
255255

256-
``` bash
256+
```bash
257257
npm install --save-dev webpack memory-fs
258258
```
259259

260260
__test/compiler.js__
261261

262-
``` js
262+
```js
263263
import path from 'path';
264264
import webpack from 'webpack';
265265
import memoryfs from 'memory-fs';
@@ -303,7 +303,7 @@ And now, finally, we can write our test and add an npm script to run it:
303303

304304
__test/loader.test.js__
305305

306-
``` js
306+
```js
307307
import compiler from './compiler.js';
308308

309309
test('Inserts name and outputs JavaScript', async () => {
@@ -316,15 +316,15 @@ test('Inserts name and outputs JavaScript', async () => {
316316

317317
__package.json__
318318

319-
``` js
319+
```js
320320
"scripts": {
321321
"test": "jest"
322322
}
323323
```
324324

325325
With everything in place, we can run it and see if our new loader passes the test:
326326

327-
``` bash
327+
```bash
328328
PASS test/loader.test.js
329329
✓ Inserts name and outputs JavaScript (229ms)
330330

src/content/plugins/normal-module-replacement-plugin.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ console.log(config.title);
7979

8080
And now you just get the right config imported depending on which target you're building for:
8181

82-
``` shell
82+
```bash
8383
webpack --env.APP_TARGET VERSION_A
8484
=> 'I am version A'
8585

webpack.common.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ module.exports = (env = {}) => ({
3737
options: {
3838
plugins: [
3939
require('./tip'),
40+
require('./highlight'),
4041
require('remark-slug'),
4142
[
4243
require('remark-autolink-headings'),

0 commit comments

Comments
 (0)