2021-02-19 00:18:45 +03:00
|
|
|
/*
|
2022-01-10 11:59:00 +03:00
|
|
|
* Copyright (c) 2016-2022 Martin Donath <martin.donath@squidfunk.com>
|
2021-02-19 00:18:45 +03:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2022-01-30 11:04:11 +03:00
|
|
|
import fonts from "google-fonts-complete"
|
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 16:58:26 +03:00
|
|
|
import {
|
2021-11-13 14:05:24 +03:00
|
|
|
EMPTY,
|
|
|
|
concat,
|
2021-02-21 16:58:26 +03:00
|
|
|
concatMap,
|
2021-11-13 14:05:24 +03:00
|
|
|
defer,
|
2021-02-21 16:58:26 +03:00
|
|
|
map,
|
2021-11-13 14:05:24 +03:00
|
|
|
merge,
|
|
|
|
of,
|
2021-02-22 20:19:00 +03:00
|
|
|
reduce,
|
2021-02-26 18:14:18 +03:00
|
|
|
scan,
|
|
|
|
startWith,
|
|
|
|
switchMap,
|
|
|
|
switchMapTo,
|
2021-11-13 14:05:24 +03:00
|
|
|
toArray,
|
|
|
|
zip
|
|
|
|
} from "rxjs"
|
2021-02-21 19:35:11 +03:00
|
|
|
import {
|
|
|
|
extendDefaultPlugins,
|
|
|
|
optimize
|
|
|
|
} from "svgo"
|
2021-02-19 00:18:45 +03:00
|
|
|
|
2021-02-22 20:19:00 +03:00
|
|
|
import { IconSearchIndex } from "_/components"
|
|
|
|
|
2021-02-26 18:14:18 +03:00
|
|
|
import { base, read, resolve, watch, write } from "./_"
|
2021-02-22 20:19:00 +03:00
|
|
|
import { copyAll } from "./copy"
|
2021-02-20 20:46:28 +03:00
|
|
|
import {
|
|
|
|
transformScript,
|
|
|
|
transformStyle
|
|
|
|
} from "./transform"
|
2021-02-19 00:18:45 +03:00
|
|
|
|
2021-02-22 20:19:00 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Helper types
|
|
|
|
* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Twemoji icon
|
|
|
|
*/
|
|
|
|
interface TwemojiIcon {
|
|
|
|
unicode: string /* Unicode code point */
|
|
|
|
}
|
|
|
|
|
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
|
|
|
/* ----------------------------------------------------------------------------
|
2021-02-26 18:14:18 +03:00
|
|
|
* Tasks
|
2021-02-19 00:18:45 +03:00
|
|
|
* ------------------------------------------------------------------------- */
|
|
|
|
|
2021-02-22 20:19:00 +03:00
|
|
|
/* Copy all assets */
|
|
|
|
const assets$ = concat(
|
2021-02-19 00:18:45 +03:00
|
|
|
|
|
|
|
/* Copy Material Design icons */
|
|
|
|
...["*.svg", "../LICENSE"]
|
|
|
|
.map(pattern => copyAll(pattern, {
|
2021-02-22 20:19:00 +03:00
|
|
|
from: "node_modules/@mdi/svg/svg",
|
|
|
|
to: `${base}/.icons/material`,
|
2021-02-26 18:14:18 +03:00
|
|
|
transform: async data => minsvg(data)
|
2021-02-19 00:18:45 +03:00
|
|
|
})),
|
|
|
|
|
|
|
|
/* Copy GitHub octicons */
|
|
|
|
...["*.svg", "../../LICENSE"]
|
|
|
|
.map(pattern => copyAll(pattern, {
|
2021-02-22 20:19:00 +03:00
|
|
|
from: "node_modules/@primer/octicons/build/svg",
|
|
|
|
to: `${base}/.icons/octicons`,
|
2021-02-26 18:14:18 +03:00
|
|
|
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-22 20:19:00 +03:00
|
|
|
from: "node_modules/@fortawesome/fontawesome-free/svgs",
|
|
|
|
to: `${base}/.icons/fontawesome`,
|
2021-02-26 18:14:18 +03:00
|
|
|
transform: async data => minsvg(data)
|
2021-02-22 20:19:00 +03:00
|
|
|
})),
|
2021-02-19 00:18:45 +03:00
|
|
|
|
2021-06-25 12:34:51 +03:00
|
|
|
/* Copy Lunr.js search stemmers and segmenters */
|
|
|
|
...["min/*.js", "tinyseg.js", "wordcut.js"]
|
2021-02-19 00:18:45 +03:00
|
|
|
.map(pattern => copyAll(pattern, {
|
2021-02-22 20:19:00 +03:00
|
|
|
from: "node_modules/lunr-languages",
|
|
|
|
to: `${base}/assets/javascripts/lunr`
|
2021-02-19 00:18:45 +03:00
|
|
|
})),
|
|
|
|
|
2021-02-22 20:19:00 +03:00
|
|
|
/* Copy images and configurations */
|
|
|
|
...[".icons/*.svg", "assets/images/*", "**/*.{py,yml}"]
|
|
|
|
.map(pattern => copyAll(pattern, {
|
|
|
|
from: "src",
|
|
|
|
to: base
|
|
|
|
}))
|
2021-02-19 00:18:45 +03:00
|
|
|
)
|
|
|
|
|
2021-02-22 20:19:00 +03:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Transform styles */
|
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-22 20:19:00 +03:00
|
|
|
concatMap(file => zip(
|
|
|
|
of(ext(file, ".css")),
|
|
|
|
transformStyle({
|
|
|
|
from: `src/${file}`,
|
|
|
|
to: ext(`${base}/${file}`, ".css")
|
|
|
|
}))
|
|
|
|
)
|
2021-02-19 00:18:45 +03:00
|
|
|
)
|
2021-02-20 20:03:53 +03:00
|
|
|
|
2021-02-22 20:19:00 +03:00
|
|
|
/* Transform scripts */
|
2021-02-21 16:34:17 +03:00
|
|
|
const javascripts$ = resolve("**/{bundle,search}.ts", { cwd: "src" })
|
|
|
|
.pipe(
|
2021-02-22 20:19:00 +03:00
|
|
|
concatMap(file => zip(
|
|
|
|
of(ext(file, ".js")),
|
|
|
|
transformScript({
|
|
|
|
from: `src/${file}`,
|
|
|
|
to: ext(`${base}/${file}`, ".js")
|
|
|
|
}))
|
|
|
|
)
|
2021-02-21 16:34:17 +03:00
|
|
|
)
|
|
|
|
|
2021-02-22 20:19:00 +03:00
|
|
|
/* Compute manifest */
|
|
|
|
const manifest$ = merge(
|
2021-02-26 18:14:18 +03:00
|
|
|
...Object.entries({
|
|
|
|
"**/*.scss": stylesheets$,
|
|
|
|
"**/*.ts*": javascripts$
|
|
|
|
})
|
|
|
|
.map(([pattern, observable$]) => (
|
|
|
|
defer(() => process.argv.includes("--watch")
|
|
|
|
? watch(pattern, { cwd: "src" })
|
|
|
|
: EMPTY
|
|
|
|
)
|
|
|
|
.pipe(
|
|
|
|
startWith("*"),
|
|
|
|
switchMapTo(observable$.pipe(toArray()))
|
|
|
|
)
|
|
|
|
))
|
2021-02-21 19:35:11 +03:00
|
|
|
)
|
2021-02-21 16:58:26 +03:00
|
|
|
.pipe(
|
2021-02-26 18:14:18 +03:00
|
|
|
scan((prev, mapping) => (
|
|
|
|
mapping.reduce((next, [key, value]) => (
|
|
|
|
next.set(key, value.replace(`${base}/`, ""))
|
|
|
|
), prev)
|
|
|
|
), new Map<string, string>()),
|
2021-02-22 20:19:00 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
/* Transform templates */
|
|
|
|
const templates$ = manifest$
|
|
|
|
.pipe(
|
|
|
|
switchMap(manifest => copyAll("**/*.html", {
|
|
|
|
from: "src",
|
|
|
|
to: base,
|
|
|
|
watch: process.argv.includes("--watch"),
|
|
|
|
transform: async data => {
|
2021-02-22 20:23:33 +03:00
|
|
|
const metadata = require("../../package.json")
|
2021-02-22 20:19:00 +03:00
|
|
|
const banner =
|
|
|
|
"{#-\n" +
|
|
|
|
" This file was automatically generated - do not edit\n" +
|
|
|
|
"-#}\n"
|
|
|
|
|
|
|
|
/* If necessary, apply manifest */
|
|
|
|
if (process.argv.includes("--optimize"))
|
|
|
|
for (const [key, value] of manifest)
|
|
|
|
data = data.replace(
|
|
|
|
new RegExp(`('|")${key}\\1`, "g"),
|
|
|
|
`$1${value}$1`
|
2021-02-21 19:35:11 +03:00
|
|
|
)
|
2021-02-22 20:19:00 +03:00
|
|
|
|
|
|
|
/* Normalize line feeds and minify HTML */
|
|
|
|
const html = data.replace(/\r\n/gm, "\n")
|
|
|
|
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-21 16:58:26 +03:00
|
|
|
)
|
2021-02-21 16:34:17 +03:00
|
|
|
|
2021-02-22 20:19:00 +03:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Compute icon mappings */
|
|
|
|
const icons$ = defer(() => resolve("**/*.svg", { cwd: "material/.icons" }))
|
|
|
|
.pipe(
|
|
|
|
reduce((index, file) => index.set(
|
2021-02-23 11:54:57 +03:00
|
|
|
file.replace(/\.svg$/, "").replace(/\//g, "-"),
|
|
|
|
file
|
2021-02-22 20:19:00 +03:00
|
|
|
), new Map<string, string>())
|
|
|
|
)
|
|
|
|
|
|
|
|
/* Compute emoji mappings (based on Twemoji) */
|
|
|
|
const emojis$ = defer(() => resolve("venv/**/twemoji_db.py"))
|
|
|
|
.pipe(
|
2021-02-26 18:14:18 +03:00
|
|
|
switchMap(file => read(file)),
|
2021-02-22 20:19:00 +03:00
|
|
|
map(data => {
|
|
|
|
const [, payload] = data.match(/^emoji = ({.*})$.alias/ms)!
|
|
|
|
return Object.entries<TwemojiIcon>(JSON.parse(payload))
|
|
|
|
.reduce((index, [name, { unicode }]) => index.set(
|
|
|
|
name.replace(/(^:|:$)/g, ""),
|
|
|
|
`${unicode}.svg`
|
|
|
|
), new Map<string, string>())
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
|
|
|
/* Build search index for icons and emojis */
|
|
|
|
const index$ = zip(icons$, emojis$)
|
|
|
|
.pipe(
|
|
|
|
map(([icons, emojis]) => {
|
|
|
|
const cdn = "https://raw.githubusercontent.com"
|
|
|
|
return {
|
|
|
|
icons: {
|
|
|
|
base: `${cdn}/squidfunk/mkdocs-material/master/material/.icons/`,
|
|
|
|
data: Object.fromEntries(icons)
|
|
|
|
},
|
|
|
|
emojis: {
|
|
|
|
base: `${cdn}/twitter/twemoji/master/assets/svg/`,
|
|
|
|
data: Object.fromEntries(emojis)
|
|
|
|
}
|
|
|
|
} as IconSearchIndex
|
|
|
|
}),
|
2021-02-26 18:14:18 +03:00
|
|
|
switchMap(data => write(
|
2021-02-24 20:02:09 +03:00
|
|
|
`${base}/overrides/assets/javascripts/iconsearch_index.json`,
|
2021-02-22 20:19:00 +03:00
|
|
|
JSON.stringify(data)
|
|
|
|
))
|
|
|
|
)
|
2021-02-20 20:46:28 +03:00
|
|
|
|
2022-01-30 11:04:11 +03:00
|
|
|
/* ------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Compute font schema */
|
|
|
|
const fonts$ = of(Object.keys(fonts))
|
|
|
|
.pipe(
|
|
|
|
map(items => ({
|
|
|
|
"$schema": "https://json-schema.org/draft-07/schema",
|
|
|
|
"title": "Google Fonts",
|
|
|
|
"markdownDescription": "https://fonts.google.com/",
|
|
|
|
"oneOf": items.map(item => ({
|
|
|
|
"title": item,
|
|
|
|
"markdownDescription": `https://fonts.google.com/specimen/${
|
|
|
|
item.replace(/\s+/g, "+")
|
|
|
|
}`,
|
|
|
|
"enum": [
|
|
|
|
item
|
|
|
|
],
|
|
|
|
}))
|
|
|
|
})),
|
|
|
|
switchMap(data => write(
|
|
|
|
"docs/schema/assets/fonts.json",
|
|
|
|
JSON.stringify(data, undefined, 2)
|
|
|
|
))
|
|
|
|
)
|
|
|
|
|
|
|
|
/* Build schema */
|
|
|
|
const schema$ = merge(fonts$)
|
|
|
|
|
2021-02-26 18:14:18 +03:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
* Program
|
|
|
|
* ------------------------------------------------------------------------- */
|
2021-02-21 16:34:17 +03:00
|
|
|
|
2021-02-26 18:14:18 +03:00
|
|
|
/* Assemble pipeline */
|
|
|
|
const build$ =
|
|
|
|
process.argv.includes("--dirty")
|
|
|
|
? templates$
|
2022-01-30 11:04:11 +03:00
|
|
|
: concat(assets$, merge(templates$, index$, schema$))
|
2021-02-26 18:14:18 +03:00
|
|
|
|
|
|
|
/* Let's get rolling */
|
|
|
|
build$.subscribe()
|