deno.land / x / lume@v2.1.4 / plugins / url.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 type Site from "../core/site.ts";
export interface Options { /** The url helper name */ names?: { url?: string; htmlUrl?: string; };}
export const defaults: Options = { names: { url: "url", htmlUrl: "htmlUrl", },};
/** * A plugin to register the filters "url" and "htmlUrl" * for normalizing URLs in the templates */export default function (userOptions?: Options) { const options = merge(defaults, userOptions);
return (site: Site) => { site.filter(options.names.url!, url); site.filter(options.names.htmlUrl!, htmlUrl);
function url(path = "/", absolute = false): string { return typeof path === "string" ? site.url(path, absolute) : path; }
function htmlUrl(html = "", absolute = false): string { return html.replaceAll( /\s(href|src)="([^"]+)"/g, (_match, attr, value) => ` ${attr}="${url(value, absolute)}"`, ); } };}
/** Extends Helpers interface */declare global { namespace Lume { export interface Helpers { /** @see https://lume.land/plugins/url/#url-filter */ url: (path: string, absolute?: boolean) => string;
/** @see https://lume.land/plugins/url/#htmlurl-filter */ htmlUrl: (html: string, absolute?: boolean) => string; } }}
lume

Version Info

Tagged at
7 months ago