mirror of
https://github.com/jackyzha0/quartz
synced 2026-11-27 22:14:40 +01:00
refactor(slugs): use @quartz-community/utils' slug options instead of core's copy (core requires utils 1.3)
This commit is contained in:
1 file changed
+12
-51
+12
-51
@@ -34,60 +34,21 @@ export type {
|
||||
|
||||
// --- v5-specific exports below ---
|
||||
|
||||
import { slugifyFilePath as slugifyFilePathBase } from "@quartz-community/utils"
|
||||
import * as quartzUtils from "@quartz-community/utils"
|
||||
import type { FilePath as _FilePath, FullSlug as _FullSlug } from "@quartz-community/utils"
|
||||
|
||||
export interface SlugOptions {
|
||||
/** Strip accents and other diacritics (`č` → `c`, `é` → `e`) from generated slugs. */
|
||||
stripDiacritics?: boolean
|
||||
}
|
||||
|
||||
let slugOptions: SlugOptions = {}
|
||||
|
||||
/**
|
||||
* Set how core turns file paths into slugs (`configuration.slugs`). Called when the config is
|
||||
* loaded, in the main thread and in every worker, so all slugs of a build agree.
|
||||
*/
|
||||
export function setSlugOptions(options: SlugOptions | undefined): void {
|
||||
slugOptions = { ...(options ?? {}) }
|
||||
// @quartz-community/utils 1.3+ applies the same options to every slug it computes, for core
|
||||
// and for plugins (tag pages, canvas, bases, alias-redirects …). Older utils lack it; core's
|
||||
// own wrapper below and the links/dispatcher handling then cover core's slugs. Stripping is
|
||||
// idempotent, so both applying is harmless. (Drop the wrapper once core requires utils 1.3.)
|
||||
const utilsSetter = (quartzUtils as { setSlugOptions?: (o: SlugOptions | undefined) => void })
|
||||
.setSlugOptions
|
||||
utilsSetter?.(slugOptions)
|
||||
}
|
||||
|
||||
export function getSlugOptions(): Readonly<SlugOptions> {
|
||||
return slugOptions
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove combining diacritical marks (U+0300–U+036F) after canonical decomposition:
|
||||
* `Čeština` → `Cestina`, `Ελληνικά` → `Ελληνικα`. The string is recomposed afterwards, so
|
||||
* scripts whose marks are outside that block (Hangul, kana with dakuten, Devanagari, Arabic)
|
||||
* keep their usual form. Letters without a decomposition (`ø`, `ł`, `ß`, `æ`) are kept.
|
||||
*/
|
||||
export function stripDiacritics(s: string): string {
|
||||
return s
|
||||
.normalize("NFD")
|
||||
.replace(/[\u0300-\u036f]/g, "")
|
||||
.normalize("NFC")
|
||||
}
|
||||
// `configuration.slugs` (stripDiacritics) lives in @quartz-community/utils, on a global shared by
|
||||
// every copy of the package, so core's slugs and plugins' slugs (tag pages, canvas, bases,
|
||||
// alias-redirects …) agree. The config loader sets it in the main thread and in every worker.
|
||||
import { getSlugOptions, stripDiacritics } from "@quartz-community/utils"
|
||||
export {
|
||||
getSlugOptions,
|
||||
setSlugOptions,
|
||||
slugifyFilePath,
|
||||
stripDiacritics,
|
||||
} from "@quartz-community/utils"
|
||||
export type { SlugOptions } from "@quartz-community/utils"
|
||||
|
||||
/** `stripDiacritics` when `configuration.slugs.stripDiacritics` is on, else unchanged. */
|
||||
export function applySlugOptions(s: string): string {
|
||||
return slugOptions.stripDiacritics ? stripDiacritics(s) : s
|
||||
}
|
||||
|
||||
/**
|
||||
* Slug of a content file path (see `@quartz-community/utils`), honouring
|
||||
* `configuration.slugs` (see setSlugOptions).
|
||||
*/
|
||||
export function slugifyFilePath(fp: _FilePath, excludeExt?: boolean): _FullSlug {
|
||||
return slugifyFilePathBase(applySlugOptions(fp) as _FilePath, excludeExt)
|
||||
return getSlugOptions().stripDiacritics ? stripDiacritics(s) : s
|
||||
}
|
||||
|
||||
export const QUARTZ = "quartz"
|
||||
|
||||
Reference in new issue
Block a user