1
0
mirror of https://github.com/squidfunk/mkdocs-material.git synced 2024-11-13 18:40:54 +01:00
mkdocs-material/tools/build/index.ts

402 lines
11 KiB
TypeScript
Raw Permalink Normal View History

2021-02-18 22:18:45 +01:00
/*
2023-01-02 11:08:58 +01:00
* Copyright (c) 2016-2023 Martin Donath <martin.donath@squidfunk.com>
2021-02-18 22:18:45 +01: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.
*/
2024-08-26 10:57:35 +02:00
import { minify as minhtml } from "html-minifier-terser"
2021-02-21 11:59:38 +01:00
import * as path from "path"
2021-02-21 14:58:26 +01:00
import {
2021-11-13 12:05:24 +01:00
EMPTY,
concat,
defer,
2024-10-08 12:32:48 +02:00
from,
2021-02-21 14:58:26 +01:00
map,
2021-11-13 12:05:24 +01:00
merge,
mergeMap,
2021-11-13 12:05:24 +01:00
of,
2021-02-22 18:19:00 +01:00
reduce,
2021-02-26 16:14:18 +01:00
scan,
startWith,
switchMap,
2021-11-13 12:05:24 +01:00
toArray,
zip
} from "rxjs"
2022-03-28 17:58:05 +02:00
import { optimize } from "svgo"
2021-02-18 22:18:45 +01:00
2021-02-22 18:19:00 +01:00
import { IconSearchIndex } from "_/components"
2021-02-26 16:14:18 +01:00
import { base, read, resolve, watch, write } from "./_"
2021-02-22 18:19:00 +01:00
import { copyAll } from "./copy"
2021-02-20 18:46:28 +01:00
import {
transformScript,
transformStyle
} from "./transform"
2024-10-08 12:32:48 +02:00
import glob from "tiny-glob"
2021-02-18 22:18:45 +01:00
2021-02-22 18:19:00 +01:00
/* ----------------------------------------------------------------------------
* Helper types
* ------------------------------------------------------------------------- */
/**
* Twemoji icon
*/
interface TwemojiIcon {
unicode: string /* Unicode code point */
}
2021-02-21 11:59:38 +01: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 14:34:17 +01: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 {
2022-11-03 14:17:01 +01:00
if (!data.startsWith("<"))
return data
/* Optimize SVG */
2021-02-21 14:34:17 +01:00
const result = optimize(data, {
2022-03-28 17:58:05 +02:00
plugins: [
2024-08-23 12:30:51 +02:00
{
name: "preset-default",
params: {
overrides: {
removeViewBox: false
}
}
},
{
name: "removeDimensions"
}
2022-03-28 17:58:05 +02:00
]
2021-02-21 14:34:17 +01:00
})
2022-11-03 14:17:01 +01:00
/* Return minified SVG */
return result.data
2021-02-21 14:34:17 +01:00
}
2021-02-18 22:18:45 +01:00
/* ----------------------------------------------------------------------------
2021-02-26 16:14:18 +01:00
* Tasks
2021-02-18 22:18:45 +01:00
* ------------------------------------------------------------------------- */
2021-02-22 18:19:00 +01:00
/* Copy all assets */
const assets$ = concat(
2021-02-18 22:18:45 +01:00
/* Copy Material Design icons */
...["*.svg", "../LICENSE"]
.map(pattern => copyAll(pattern, {
2021-02-22 18:19:00 +01:00
from: "node_modules/@mdi/svg/svg",
2023-09-20 13:58:03 +02:00
to: `${base}/templates/.icons/material`,
2021-02-26 16:14:18 +01:00
transform: async data => minsvg(data)
2021-02-18 22:18:45 +01:00
})),
/* Copy GitHub octicons */
...["*.svg", "../../LICENSE"]
.map(pattern => copyAll(pattern, {
2021-02-22 18:19:00 +01:00
from: "node_modules/@primer/octicons/build/svg",
2023-09-20 13:58:03 +02:00
to: `${base}/templates/.icons/octicons`,
2021-02-26 16:14:18 +01:00
transform: async data => minsvg(data)
2021-02-18 22:18:45 +01:00
})),
/* Copy FontAwesome icons */
...["**/*.svg", "../LICENSE.txt"]
.map(pattern => copyAll(pattern, {
2021-02-22 18:19:00 +01:00
from: "node_modules/@fortawesome/fontawesome-free/svgs",
2023-09-20 13:58:03 +02:00
to: `${base}/templates/.icons/fontawesome`,
2021-02-26 16:14:18 +01:00
transform: async data => minsvg(data)
2021-02-22 18:19:00 +01:00
})),
2021-02-18 22:18:45 +01:00
2022-09-07 19:34:35 +02:00
/* Copy Simple icons */
...["**/*.svg", "../LICENSE.md"]
.map(pattern => copyAll(pattern, {
from: "node_modules/simple-icons/icons",
2023-09-20 13:58:03 +02:00
to: `${base}/templates/.icons/simple`,
2022-09-07 19:34:35 +02:00
transform: async data => minsvg(data)
})),
2021-06-25 11:34:51 +02:00
/* Copy Lunr.js search stemmers and segmenters */
...["min/*.js", "tinyseg.js", "wordcut.js"]
2021-02-18 22:18:45 +01:00
.map(pattern => copyAll(pattern, {
2021-02-22 18:19:00 +01:00
from: "node_modules/lunr-languages",
2023-09-20 13:58:03 +02:00
to: `${base}/templates/assets/javascripts/lunr`
2021-02-18 22:18:45 +01:00
})),
2021-02-22 18:19:00 +01:00
/* Copy images and configurations */
...["**/*.{jpg,png,svg,yml}"]
2021-02-22 18:19:00 +01:00
.map(pattern => copyAll(pattern, {
from: "src",
to: base
}))
2021-02-18 22:18:45 +01:00
)
/* Copy plugins and extensions */
const sources$ = copyAll("**/*.py", {
from: "src",
to: base,
2023-09-20 17:59:51 +02:00
watch: process.argv.includes("--watch"),
transform: async (data, name) => {
if (path.basename(name) === "__init__.py") {
const metadata = require("../../package.json")
return data.replace("$md-version$", metadata.version)
} else {
return data
}
}
})
2021-02-22 18:19:00 +01:00
/* ------------------------------------------------------------------------- */
/* Transform styles */
2021-02-21 11:59:38 +01:00
const stylesheets$ = resolve("**/[!_]*.scss", { cwd: "src" })
2021-02-18 22:18:45 +01:00
.pipe(
mergeMap(file => zip(
2023-09-20 14:26:05 +02:00
of(ext(file, ".css").replace(/(overrides|templates)\//, "")),
2021-02-22 18:19:00 +01:00
transformStyle({
from: `src/${file}`,
to: ext(`${base}/${file}`, ".css")
}))
)
2021-02-18 22:18:45 +01:00
)
2021-02-20 18:03:53 +01:00
2021-02-22 18:19:00 +01:00
/* Transform scripts */
const javascripts$ = resolve("**/{custom,bundle,search}.ts", { cwd: "src" })
2021-02-21 14:34:17 +01:00
.pipe(
mergeMap(file => zip(
2023-09-20 14:26:05 +02:00
of(ext(file, ".js").replace(/(overrides|templates)\//, "")),
2021-02-22 18:19:00 +01:00
transformScript({
from: `src/${file}`,
to: ext(`${base}/${file}`, ".js")
}))
)
2021-02-21 14:34:17 +01:00
)
2021-02-22 18:19:00 +01:00
/* Compute manifest */
const manifest$ = merge(
2021-02-26 16:14:18 +01:00
...Object.entries({
"**/*.scss": stylesheets$,
"**/*.ts*": javascripts$
})
.map(([pattern, observable$]) => (
defer(() => process.argv.includes("--watch")
2024-10-08 12:32:48 +02:00
? from(glob(pattern, { cwd: "src" })).pipe(
switchMap(files => watch(files, { cwd: "src" }))
)
2021-02-26 16:14:18 +01:00
: EMPTY
)
.pipe(
startWith("*"),
2022-04-02 18:36:59 +02:00
switchMap(() => observable$.pipe(toArray()))
2021-02-26 16:14:18 +01:00
)
))
2021-02-21 17:35:11 +01:00
)
2021-02-21 14:58:26 +01:00
.pipe(
2021-02-26 16:14:18 +01:00
scan((prev, mapping) => (
mapping.reduce((next, [key, value]) => (
next.set(key, value.replace(
2023-09-20 14:26:05 +02:00
new RegExp(`${base}\\/(overrides|templates)\\/`),
""
))
2021-02-26 16:14:18 +01:00
), prev)
), new Map<string, string>()),
2021-02-22 18:19:00 +01: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 18:23:33 +01:00
const metadata = require("../../package.json")
2021-02-22 18:19:00 +01: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 17:35:11 +01:00
)
2021-02-22 18:19:00 +01:00
/* Normalize line feeds and minify HTML */
const html = data.replace(/\r\n/gm, "\n")
2024-08-26 10:57:35 +02:00
return banner + await minhtml(html, {
2021-02-22 18:19:00 +01:00
collapseBooleanAttributes: true,
includeAutoGeneratedTags: false,
minifyCSS: true,
minifyJS: true,
removeComments: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true
})
2024-08-26 10:57:35 +02:00
.then(html => html
2021-02-22 18:19:00 +01:00
2024-08-26 10:57:35 +02:00
/* Remove empty lines without collapsing everything */
.replace(/^\s*[\r\n]/gm, "")
2021-02-22 18:19:00 +01:00
2024-08-26 10:57:35 +02:00
/* Write theme version into template */
.replace("$md-name$", metadata.name)
.replace("$md-version$", metadata.version)
)
2021-02-22 18:19:00 +01:00
}
}))
2021-02-21 14:58:26 +01:00
)
2021-02-21 14:34:17 +01:00
2021-02-22 18:19:00 +01:00
/* ------------------------------------------------------------------------- */
/* Compute icon mappings */
2023-09-20 13:58:03 +02:00
const icons$ = defer(() => resolve("**/*.svg", {
cwd: `${base}/templates/.icons`
}))
2021-02-22 18:19:00 +01:00
.pipe(
reduce((index, file) => index.set(
2021-02-23 09:54:57 +01:00
file.replace(/\.svg$/, "").replace(/\//g, "-"),
file
2021-02-22 18:19:00 +01:00
), new Map<string, string>())
)
/* Compute emoji mappings (based on Twemoji) */
const emojis$ = defer(() => resolve("venv/**/twemoji_db.py"))
.pipe(
2021-02-26 16:14:18 +01:00
switchMap(file => read(file)),
2021-02-22 18:19:00 +01: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: {
2023-09-20 14:59:25 +02:00
base: `${cdn}/squidfunk/mkdocs-material/master/material/templates/.icons/`,
2021-02-22 18:19:00 +01:00
data: Object.fromEntries(icons)
},
emojis: {
base: `${cdn}/twitter/twemoji/master/assets/svg/`,
data: Object.fromEntries(emojis)
}
} as IconSearchIndex
}),
2021-02-26 16:14:18 +01:00
switchMap(data => write(
2023-09-20 13:58:03 +02:00
`${base}/overrides/assets/javascripts/iconsearch_index.json`,
2021-02-22 18:19:00 +01:00
JSON.stringify(data)
))
)
2021-02-20 18:46:28 +01:00
2022-01-30 09:04:11 +01:00
/* ------------------------------------------------------------------------- */
/* Build schema */
2022-01-30 15:04:20 +01:00
const schema$ = merge(
/* Compute fonts schema */
defer(() => import("google-fonts-complete"))
.pipe(
2022-01-30 15:10:51 +01:00
map(({ default: fonts }) => Object.keys(fonts)),
2022-01-30 15:04:20 +01:00
map(fonts => ({
"$schema": "https://json-schema.org/draft-07/schema",
"title": "Google Fonts",
"markdownDescription": "https://fonts.google.com/",
"type": "string",
"oneOf": fonts.map(font => ({
"title": font,
"markdownDescription": `https://fonts.google.com/specimen/${
font.replace(/\s+/g, "+")
}`,
"const": font,
2022-01-30 15:04:20 +01:00
}))
})),
switchMap(data => write(
"docs/schema/assets/fonts.json",
JSON.stringify(data, undefined, 2)
))
),
/* Compute icons schema */
icons$
.pipe(
map(icons => [...icons.values()]),
map(icons => ({
"$schema": "https://json-schema.org/draft-07/schema",
"title": "Icon",
"markdownDescription": [
"https://squidfunk.github.io/mkdocs-material",
"reference/icons-emojis/#search"
].join("/"),
"type": "string",
"enum": icons.map(icon => icon.replace(".svg", ""))
})),
switchMap(data => write(
"docs/schema/assets/icons.json",
JSON.stringify(data, undefined, 2)
))
)
)
2022-01-30 09:04:11 +01:00
/* Build overrides */
const overrides$ =
process.argv.includes("--all")
? merge(index$, schema$)
: EMPTY
2021-02-26 16:14:18 +01:00
/* ----------------------------------------------------------------------------
* Program
* ------------------------------------------------------------------------- */
2021-02-21 14:34:17 +01:00
2021-02-26 16:14:18 +01:00
/* Assemble pipeline */
const build$ =
process.argv.includes("--dirty")
? merge(templates$, sources$)
: concat(assets$, merge(templates$, sources$, overrides$))
2021-02-26 16:14:18 +01:00
/* Let's get rolling */
build$.subscribe()