Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
ccjmne committed Oct 14, 2024
1 parent e58a139 commit ec6a3ce
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/utils/easy-htmlelement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import externalLink from 'src/assets/external-link.svg?template'
import { type RegExpGroups } from 'src/types'

const SVGNS = 'http://www.w3.org/2000/svg'
const HYPHENATE = /^y/i.test(process.env.HYPHENATE ?? 'no')

export default class EasyHTMLElement {

Expand Down Expand Up @@ -49,20 +50,27 @@ export default class EasyHTMLElement {
/**
* Parses contents and:
* - discard empty strings
* - automatically mark for hyphenation (for `en-gb`) with `\u00AD` (soft hyphen),
* - replace linefeeds (literal `\n`) with `<br />` elements
* - replace markdown-style hyperlinks with `<a href="...">...</a>` elements
* - replace `&nbsp;` with `\u00A0` (non-breaking space)
* - replace `&shy;` with `\u00AD` (soft hyphen)
*
* Additionally, while in `development` mode, automatically mark for
* hyphenation (for `en-gb`) with `\u00AD` (soft hyphen).
*
* This isn't done in `production` builds so as to avoid needlessly confusing
* crawlers and possible ATSs (Applicant Tracking System).
*/
private static prepare(elements: ReadonlyArray<string | EasyHTMLElement>): ReadonlyArray<string | HTMLElement | SVGElement> {
return elements
.filter(content => content !== '')
.flatMap(content => (typeof content !== 'string' ? content.elem : content
.replace(/&nbsp;/g, '\u00A0')
.replace(/&shy;/g, '\u00AD')
.split(/(?<=\[[^\]]+\]\([^)]+\))|(?=\[[^\]]+\]\([^)]+\))/) // split around markdown-style hyperlinks
.map(fragment => fragment.match(/^\[(?<text>[^\]]+)\]\((?<href>[^)]+)\)$/)?.groups as RegExpGroups<'text' | 'href'> | undefined ?? fragment)
.flatMap(fragment => (typeof fragment === 'string'
? fragment.split(/\n/g).flatMap(t => [new EasyHTMLElement('br').elem, hyphenate(t)]).slice(1)
? fragment.split(/\n/g).flatMap(t => [new EasyHTMLElement('br').elem, HYPHENATE ? hyphenate(t) : t]).slice(1)
: EasyHTMLElement.anchor(fragment).elem))
))
}
Expand Down
8 changes: 5 additions & 3 deletions webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Compiler } from 'webpack'
import 'webpack-dev-server' // Augment "Configuration" type
import { author, description, homepage, keywords, name, repository, title } from './package.json'
import { PDFPrinter } from './tooling/pdf-printer-plugin'
import { DefinePlugin } from 'webpack'

const src = resolve(__dirname, 'src')
const dist = resolve(__dirname, 'dist')
Expand All @@ -32,7 +33,7 @@ const pages = readdirSync(src, { withFileTypes: true })
.reduce((acc, { name, path }) => ({ ...acc, [name]: path }), {})

export default (
_env: string,
env: NodeJS.ProcessEnv,
{ mode = 'production', port = '8042' }: { mode?: 'production' | 'development', port?: string } = {},
): Configuration => ({
entry: pages,
Expand Down Expand Up @@ -89,9 +90,10 @@ export default (
path: dist,
},
plugins: [
new DefinePlugin({ 'process.env': JSON.stringify(env) }),
...mode === 'production' ? [new CleanWebpackPlugin()] : [],
new TypedScssModulesPlugin({ watch: mode === 'development' }),
...Object.entries(pages).map(([name, path]) => new HtmlWebpackPlugin({
...Object.keys(pages).map(name => new HtmlWebpackPlugin({
title: `Page ${name}`,
meta: { author, description, repository, keywords: keywords.join(', ') },
chunks: [name],
Expand All @@ -102,7 +104,7 @@ export default (
...mode === 'production'
? { scheme: 'file', paths: Object.keys(pages).map(name => resolve(dist, `${name}.html`)) }
: { port, paths: Object.keys(pages).map(name => `${name}.html`) },
output: resolve(dist, `${out}.pdf`),
output: resolve(dist, env.OUTPUT ?? `${out}.pdf`),
properties: { title, author, subject: description, keywords: keywords.join(', '), creator: `${name} (${homepage})` },
blocking: mode === 'production',
}),
Expand Down

0 comments on commit ec6a3ce

Please sign in to comment.