mkdocs-material/tools/index.ts

253 lines
7.3 KiB
TypeScript
Raw Normal View History

2021-02-19 00:18:45 +03:00
/*
* Copyright (c) 2016-2021 Martin Donath <martin.donath@squidfunk.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
2021-02-21 16:58:26 +03:00
import { createHash } from "crypto"
import * as fs from "fs/promises"
2021-02-19 00:18:45 +03:00
import { minify as minhtml } from "html-minifier"
2021-02-21 13:59:38 +03:00
import * as path from "path"
2021-02-21 19:35:11 +03:00
import { EMPTY, concat, defer, from, merge, of } from "rxjs"
2021-02-21 16:58:26 +03:00
import {
concatMap,
map,
2021-02-21 19:35:11 +03:00
mergeMap,
2021-02-21 16:58:26 +03:00
switchMap,
2021-02-21 19:35:11 +03:00
toArray
2021-02-21 16:58:26 +03:00
} from "rxjs/operators"
2021-02-21 19:35:11 +03:00
import {
extendDefaultPlugins,
optimize
} from "svgo"
2021-02-19 00:18:45 +03:00
2021-02-21 19:35:11 +03:00
import { copy, copyAll } from "./copy"
import { base, cachebust, resolve } from "./resolve"
2021-02-20 20:46:28 +03:00
import {
transformScript,
transformStyle
} from "./transform"
2021-02-19 00:18:45 +03:00
2021-02-21 13:59:38 +03:00
/* ----------------------------------------------------------------------------
* Helper functions
* ------------------------------------------------------------------------- */
/**
* Replace file extension
*
* @param file - File
* @param extension - New extension
*
* @returns File with new extension
*/
function ext(file: string, extension: string): string {
return file.replace(path.extname(file), extension)
}
2021-02-21 16:34:17 +03:00
/**
* Optimize SVG data
*
* This function will just pass-through non-SVG data, which makes the pipeline
* much simpler, as we can reuse it for the license texts.
*
* @param data - SVG data
*
* @returns Minified SVG data
*/
function minsvg(data: string): string {
const result = optimize(data, {
plugins: extendDefaultPlugins([
{ name: "removeDimensions", active: true },
{ name: "removeViewBox", active: false }
])
})
return result.data || data
}
2021-02-19 00:18:45 +03:00
/* ----------------------------------------------------------------------------
* Program
* ------------------------------------------------------------------------- */
2021-02-20 20:03:53 +03:00
/* Copy all dependencies */
const dependencies$ = concat(
2021-02-19 00:18:45 +03:00
/* Copy Material Design icons */
...["*.svg", "../LICENSE"]
.map(pattern => copyAll(pattern, {
2021-02-20 20:03:53 +03:00
src: "node_modules/@mdi/svg/svg",
2021-02-21 16:34:17 +03:00
out: `${base}/.icons/material`,
...process.argv.includes("--optimize") && {
transform: async data => minsvg(data)
}
2021-02-19 00:18:45 +03:00
})),
/* Copy GitHub octicons */
...["*.svg", "../../LICENSE"]
.map(pattern => copyAll(pattern, {
2021-02-20 20:03:53 +03:00
src: "node_modules/@primer/octicons/build/svg",
2021-02-21 16:34:17 +03:00
out: `${base}/.icons/octicons`,
...process.argv.includes("--optimize") && {
transform: async data => minsvg(data)
}
2021-02-19 00:18:45 +03:00
})),
/* Copy FontAwesome icons */
...["**/*.svg", "../LICENSE.txt"]
.map(pattern => copyAll(pattern, {
2021-02-20 20:03:53 +03:00
src: "node_modules/@fortawesome/fontawesome-free/svgs",
2021-02-21 16:34:17 +03:00
out: `${base}/.icons/fontawesome`,
...process.argv.includes("--optimize") && {
transform: async data => minsvg(data)
}
2021-02-19 00:18:45 +03:00
}))
)
2021-02-20 20:03:53 +03:00
/* Copy all assets */
2021-02-19 00:18:45 +03:00
const assets$ = concat(
2021-02-20 20:03:53 +03:00
/* Copy icons, images and configurations */
2021-02-19 00:18:45 +03:00
...[".icons/*.svg", "assets/images/*", "**/*.{py,yml}"]
.map(pattern => copyAll(pattern, {
2021-02-20 20:03:53 +03:00
src: "src",
out: base
2021-02-19 00:18:45 +03:00
})),
2021-02-20 20:03:53 +03:00
/* Copy and minify template files */
copyAll("**/*.html", {
src: "src",
out: base,
2021-02-21 16:34:17 +03:00
transform: async data => {
2021-02-20 20:03:53 +03:00
const metadata = require("../package.json")
const banner =
"{#-\n" +
" This file was automatically generated - do not edit\n" +
"-#}\n"
/* Normalize line feeds and minify HTML */
2021-02-21 16:34:17 +03:00
const html = data.replace(/\r\n/gm, "\n")
2021-02-20 20:03:53 +03:00
return banner + minhtml(html, {
collapseBooleanAttributes: true,
includeAutoGeneratedTags: false,
minifyCSS: true,
minifyJS: true,
removeComments: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true
})
/* Remove empty lines without collapsing everything */
.replace(/^\s*[\r\n]/gm, "")
/* Write theme version into template */
.replace("$md-name$", metadata.name)
.replace("$md-version$", metadata.version)
}
})
2021-02-19 00:18:45 +03:00
)
2021-02-20 20:03:53 +03:00
/* Transform stylesheets with SASS and PostCSS */
2021-02-21 13:59:38 +03:00
const stylesheets$ = resolve("**/[!_]*.scss", { cwd: "src" })
2021-02-19 00:18:45 +03:00
.pipe(
2021-02-20 20:03:53 +03:00
concatMap(file => transformStyle({
src: `src/${file}`,
2021-02-21 13:59:38 +03:00
out: ext(`${base}/${file}`, ".css")
2021-02-20 20:03:53 +03:00
}))
2021-02-19 00:18:45 +03:00
)
2021-02-20 20:03:53 +03:00
2021-02-21 16:34:17 +03:00
/* Transform stylesheets with SASS and PostCSS */
const javascripts$ = resolve("**/{bundle,search}.ts", { cwd: "src" })
.pipe(
concatMap(file => transformScript({
src: `src/${file}`,
out: ext(`${base}/${file}`, ".js")
}))
)
2021-02-21 16:58:26 +03:00
/* Add content hashes to assets and replace occurrences */
2021-02-21 19:35:11 +03:00
const manifest$ = defer(() => process.argv.includes("--optimize")
? resolve("**/*.{css,js}", { cwd: base })
: EMPTY
)
2021-02-21 16:58:26 +03:00
.pipe(
2021-02-21 19:35:11 +03:00
concatMap(file => from(fs.readFile(`${base}/${file}`, "utf8"))
2021-02-21 16:58:26 +03:00
.pipe(
map(data => createHash("sha256").update(data).digest("hex")),
2021-02-21 19:35:11 +03:00
switchMap(hash => of(`${file}`, `${file}.map`)
2021-02-21 16:58:26 +03:00
.pipe(
2021-02-21 19:35:11 +03:00
concatMap(part => cachebust(part, hash, { cwd: base }))
2021-02-21 16:58:26 +03:00
)
)
)
2021-02-21 19:35:11 +03:00
),
toArray(),
map(tuples => new Map(tuples)),
mergeMap(manifest => concat(
// TODO: split this into two. manifest + cachebust!
...["base.html", "overrides/main.html"]
.map(file => copy({
src: `${base}/${file}`,
out: `${base}/${file}`,
transform: async data => [...manifest.entries()]
.reduce((content, [key, value]) => content
.replace(
new RegExp(`('|")${key}\\1`, "g"),
`$1${value}$1`
),
data
)
})),
// TODO: interate this into the actual compilation...
...[...manifest.keys()]
.filter(file => !file.endsWith(".map"))
.map(file => copy({
src: `${base}/${manifest.get(file)!}`,
out: `${base}/${manifest.get(file)!}`,
transform: async data => data.replace(
path.basename(file),
path.basename(manifest.get(file)!),
)
}))
))
2021-02-21 16:58:26 +03:00
)
2021-02-21 16:34:17 +03:00
/* Copy Lunr.js search stemmers and segmenter */
const stemmers$ = ["min/*.js", "tinyseg.js"]
.map(pattern => copyAll(pattern, {
src: "node_modules/lunr-languages",
out: `${base}/assets/javascripts/lunr`
}))
2021-02-20 20:46:28 +03:00
2021-02-21 16:34:17 +03:00
/* ------------------------------------------------------------------------- */
/* Put everything together */
2021-02-20 20:03:53 +03:00
concat(
dependencies$,
2021-02-21 13:59:38 +03:00
merge(
assets$,
stylesheets$,
javascripts$
2021-02-21 16:34:17 +03:00
),
manifest$,
stemmers$
2021-02-20 20:03:53 +03:00
)
2021-02-21 13:59:38 +03:00
.subscribe()
// .subscribe(console.log)