forked from ffoodd/a11y.css
-
Notifications
You must be signed in to change notification settings - Fork 0
/
a11y.css.js
140 lines (118 loc) · 4.5 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
const fs = require('fs')
const path = require('path')
const showdown = require('showdown')
const fm = require('front-matter')
const prism = require('prismjs')
const postcss = require('postcss')
const atImport = require('postcss-import')
const uglify = require('uglify-es')
const cssnano = require('cssnano')
const DIRECTORIES = {
css: {
input: './css/'
},
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
DIRECTORIES.css.output = DIRECTORIES.static + 'css/'
const parseAssets = () => {
/**
* We need some of a11y.css compiled stylesheets to be available in the templates
* 1. Avoid crash if output directory does not exists
* 2. Then copy/paste all the files (manually for now, because of naming inconsistencies)
*/
if (!fs.existsSync(DIRECTORIES.css.output)) {
fs.mkdirSync(DIRECTORIES.css.output)
}
fs.copyFileSync(DIRECTORIES.css.input + 'a11y-en_advices-only.css', DIRECTORIES.css.output + 'a11y-en_advices-only.css')
fs.copyFileSync(DIRECTORIES.css.input + 'a11y-en_errors-only.css', DIRECTORIES.css.output + 'a11y-en_errors-only.css')
fs.copyFileSync(DIRECTORIES.css.input + 'a11y-en_obsolete-only.css', DIRECTORIES.css.output + 'a11y-en_obsoletes-only.css')
fs.copyFileSync(DIRECTORIES.css.input + 'a11y-en_warnings-only.css', DIRECTORIES.css.output + 'a11y-en_warnings-only.css')
/**
* Process docs styles and scripts
*/
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)
let processedContent = new showdown.Converter().makeHtml(content.body)
const htmlRegex = /((?<=<code class="html language-html">)(.[\s\S]+?)(?=<\/code>))/gm
let htmlContent = processedContent.match(htmlRegex)
htmlContent = String(htmlContent).replace(/(<)+/g, '<')
htmlContent = htmlContent.replace(/(>)+/g, '>')
let processedHTML = prism.highlight(htmlContent, prism.languages.html, 'html')
const cssRegex = /((?<=<code class="css language-css">)(.[\s\S]+?)(?=<\/code>))/gm
let cssContent = processedContent.match(cssRegex)
let processedCSS = prism.highlight(String(cssContent), prism.languages.css, 'css')
processedContent = processedContent.replace(htmlRegex, processedHTML)
processedContent = processedContent.replace(cssRegex, processedCSS)
return {
attributes: content.attributes,
body: processedContent
}
}
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()
}