deno.land / x / lume@v2.1.4 / plugins / slugify_urls.ts

slugify_urls.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { merge } from "../core/utils/object.ts";import { matchExtension } from "../core/utils/path.ts";import createSlugifier, { defaults as slugifierDefaults,} from "../core/slugifier.ts";
import type Site from "../core/site.ts";import type { Page } from "../core/file.ts";import type { Extensions } from "../core/utils/path.ts";import type { Options as SlugifierOptions } from "../core/slugifier.ts";
export interface Options extends SlugifierOptions { /** The list of extensions this plugin applies to */ extensions?: Extensions;}
// Default optionsexport const defaults: Options = { extensions: [".html"], ...slugifierDefaults,};
/** A plugin to slugify all URLs, replacing non-URL-safe characters */export default function (userOptions?: Options) { const options = merge(defaults, userOptions); const slugify = createSlugifier(options);
return (site: Site) => { site.filter("slugify", slugify); site.preprocess(options.extensions, (pages) => pages.forEach(slugifyUrls));
// Slugify the static files site.addEventListener("beforeRender", () => { site.files .filter((file) => matchExtension(options.extensions, file.outputPath)) .forEach((file) => file.outputPath = slugify(file.outputPath)); }); };
function slugifyUrls(page: Page) { if (typeof page.data.url === "string") { page.data.url = slugify(page.data.url); } }}
/** Extends Helpers interface */declare global { namespace Lume { export interface Helpers { /** @see https://lume.land/plugins/slugify_urls/ */ slugify: (string: string) => string; } }}
lume

Version Info

Tagged at
7 months ago