1 | 'use strict';
|
---|
2 | /*
|
---|
3 | * Quick and dirty script to build javascript stylesheets from highlight.js css
|
---|
4 | */
|
---|
5 | const path = require('path');
|
---|
6 | const fs = require('fs');
|
---|
7 | const css = require('css');
|
---|
8 | const camel = require('to-camel-case');
|
---|
9 | const autogenMessage =
|
---|
10 | '//\n// This file has been auto-generated by the `npm run build-styles-hljs` task\n//\n\n';
|
---|
11 |
|
---|
12 | fs.readdir(
|
---|
13 | path.join(__dirname, '../node_modules/highlight.js/styles'),
|
---|
14 | (err, files) => {
|
---|
15 | if (err) {
|
---|
16 | throw err;
|
---|
17 | }
|
---|
18 |
|
---|
19 | files.forEach(file => {
|
---|
20 | if (file.includes('.css')) {
|
---|
21 | createJavascriptStyleSheet(file);
|
---|
22 | }
|
---|
23 | });
|
---|
24 | const onlyCSSFiles = files.filter(file => file.includes('.css'));
|
---|
25 | const availableStyleNames = onlyCSSFiles.map(file =>
|
---|
26 | file.split('.css')[0] === 'default'
|
---|
27 | ? 'default-style'
|
---|
28 | : file.split('.css')[0]
|
---|
29 | );
|
---|
30 | const styles = availableStyleNames.map(name => `\n* ${camel(name)}`);
|
---|
31 | const defaultExports = availableStyleNames.map(
|
---|
32 | name => `export { default as ${camel(name)} } from './${name}';\n`
|
---|
33 | );
|
---|
34 | const styleMD = `## Available \`stylesheet\` props ${styles.join('')}`;
|
---|
35 | fs.writeFile(
|
---|
36 | path.join(__dirname, '../AVAILABLE_STYLES_HLJS.MD'),
|
---|
37 | styleMD,
|
---|
38 | err => {
|
---|
39 | if (err) {
|
---|
40 | throw err;
|
---|
41 | }
|
---|
42 | }
|
---|
43 | );
|
---|
44 | fs.writeFile(
|
---|
45 | path.join(__dirname, '../src/styles/hljs/index.js'),
|
---|
46 | defaultExports.join(''),
|
---|
47 | err => {
|
---|
48 | if (err) {
|
---|
49 | throw err;
|
---|
50 | }
|
---|
51 | }
|
---|
52 | );
|
---|
53 |
|
---|
54 | const demoStylesArray = `${autogenMessage}export default [${availableStyleNames
|
---|
55 | .sort()
|
---|
56 | .map(style => `\n '${style}'`)}\n];`;
|
---|
57 | fs.writeFile(
|
---|
58 | path.join(__dirname, '../demo/styles/hljs.js'),
|
---|
59 | demoStylesArray,
|
---|
60 | err => {
|
---|
61 | if (err) {
|
---|
62 | throw err;
|
---|
63 | }
|
---|
64 | }
|
---|
65 | );
|
---|
66 | }
|
---|
67 | );
|
---|
68 |
|
---|
69 | function createJavascriptStyleSheet(file) {
|
---|
70 | const ignoreStyleWithThis = '.hljs a';
|
---|
71 | const fileWithoutCSS =
|
---|
72 | file.split('.css')[0] === 'default'
|
---|
73 | ? 'default-style'
|
---|
74 | : file.split('.css')[0];
|
---|
75 | fs.readFile(
|
---|
76 | path.join(__dirname, `../node_modules/highlight.js/styles/${file}`),
|
---|
77 | 'utf-8',
|
---|
78 | (err, data) => {
|
---|
79 | if (err) {
|
---|
80 | throw err;
|
---|
81 | }
|
---|
82 | const javacriptStylesheet = css
|
---|
83 | .parse(data)
|
---|
84 | .stylesheet.rules.reduce((sheet, rule) => {
|
---|
85 | if (rule.type === 'rule') {
|
---|
86 | const style = rule.selectors.reduce((selectors, selector) => {
|
---|
87 | if (!selector.includes(ignoreStyleWithThis)) {
|
---|
88 | const selectorObject = rule.declarations.reduce(
|
---|
89 | (declarations, declaration) => {
|
---|
90 | if (
|
---|
91 | declaration.type === 'declaration' &&
|
---|
92 | declaration.property
|
---|
93 | ) {
|
---|
94 | declarations[camel(declaration.property)] =
|
---|
95 | declaration.value;
|
---|
96 | }
|
---|
97 | return declarations;
|
---|
98 | },
|
---|
99 | {}
|
---|
100 | );
|
---|
101 | selectors[selector.substring(1)] = selectorObject;
|
---|
102 | }
|
---|
103 | return selectors;
|
---|
104 | }, {});
|
---|
105 | sheet = Object.keys(style).reduce((stylesheet, selector) => {
|
---|
106 | if (stylesheet[selector]) {
|
---|
107 | stylesheet[selector] = Object.assign(
|
---|
108 | {},
|
---|
109 | stylesheet[selector],
|
---|
110 | style[selector]
|
---|
111 | );
|
---|
112 | } else {
|
---|
113 | stylesheet[selector] = style[selector];
|
---|
114 | }
|
---|
115 | return stylesheet;
|
---|
116 | }, sheet);
|
---|
117 | }
|
---|
118 | return sheet;
|
---|
119 | }, {});
|
---|
120 | fs.writeFile(
|
---|
121 | path.join(__dirname, `../src/styles/hljs/${fileWithoutCSS}.js`),
|
---|
122 | `export default ${JSON.stringify(javacriptStylesheet, null, 4)}`,
|
---|
123 | err => {
|
---|
124 | if (err) {
|
---|
125 | throw err;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | );
|
---|
129 | }
|
---|
130 | );
|
---|
131 | }
|
---|