forked from ffoodd/a11y.css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
a11y.css.js
104 lines (87 loc) · 2.79 KB
/
a11y.css.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
const fs = require('fs')
const path = require('path')
const showdown = require('showdown')
const fm = require('front-matter')
const postcss = require('postcss')
const atImport = require('postcss-import')
const uglify = require('uglify-js')
const cssnano = require('cssnano')
const DIRECTORIES = {
sass: {
input: './sass/themes/',
output: './src/_data/sass/'
},
assets: {
css: {
input: './src/assets/css/',
},
js: {
input: './src/assets/js/'
}
},
static: './src/static/'
}
DIRECTORIES.assets.css.output = DIRECTORIES.static
DIRECTORIES.assets.js.output = DIRECTORIES.static
const parseAssets = () => {
const CSS_INPUT = DIRECTORIES.assets.css.input + 'docs.css'
const CSS = fs.readFileSync(CSS_INPUT, 'utf8')
const JS = fs.readFileSync(DIRECTORIES.assets.js.input + 'docs.js', 'utf8')
// Parse and write CSS output file
postcss([atImport, cssnano])
.process(CSS, {
from: CSS_INPUT
})
.then(result => {
fs.writeFileSync(DIRECTORIES.assets.css.output + 'docs.css', result.css)
})
// Uglify and write JS output file
fs.writeFileSync(DIRECTORIES.assets.js.output + 'docs.js', uglify.minify(JS).code)
}
const processSassDocumentation = file => {
const inputFileExtension = path.extname(file)
const inputFilename = path.basename(file, inputFileExtension)
const excludeFiles = ['_all']
// Exclude files that we don't want to process
if (inputFileExtension !== '.scss' || excludeFiles.includes(inputFilename)) {
return
}
const content = fs.readFileSync(file, 'utf8')
const commentBlockRegex = /\/\*doc(.)*?\*\//gs
const comments = Array.from(content.matchAll(commentBlockRegex), data => {
return parseSassComment(data[0])
})
// Avoid crash if output directory does not exists
if (!fs.existsSync(DIRECTORIES.sass.output)) {
fs.mkdirSync(DIRECTORIES.sass.output)
}
// Write Eleventy data files
fs.writeFileSync(
`${DIRECTORIES.sass.output}/${inputFilename.replace('_', '')}.json`,
JSON.stringify(comments, null, 2)
)
}
const parseSassComment = comment => {
// Remove CSS comments syntax
comment = comment.replace(/(\/\*doc|\*\/)/g, '').trim()
const content = fm(comment)
return {
attributes: content.attributes,
body: new showdown.Converter().makeHtml(content.body)
}
}
const generateJsonDocumentation = () => {
/**
* Remove output directory before creating it again
* @note This is an experimental feature and requires Node v12.10.0 at least
* @see https://nodejs.org/api/fs.html#fs_fs_rmdirsync_path_options
*/
fs.rmdirSync(DIRECTORIES.sass.output, { recursive: true })
fs.readdirSync(DIRECTORIES.sass.input).forEach(file => {
processSassDocumentation(DIRECTORIES.sass.input + file)
})
}
module.exports = function () {
parseAssets()
generateJsonDocumentation()
}