-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
65 lines (55 loc) · 1.52 KB
/
Copy pathindex.ts
File metadata and controls
65 lines (55 loc) · 1.52 KB
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
57
58
59
60
61
62
63
64
65
import { links } from "./links";
import { meta } from "./meta";
import { normalizeMetadata } from "./normalize";
import type { InputMetadata, OutputLinks, OutputMeta } from "./types/io";
type OutputMetadata = {
meta: OutputMeta;
links: OutputLinks;
};
export function generateMetadata(metadata: InputMetadata): OutputMetadata {
const normalizedMetadata = normalizeMetadata(metadata);
return {
meta: meta(normalizedMetadata),
links: links(normalizedMetadata),
};
}
type GeneratorInputMetadata = Omit<InputMetadata, "title"> & {
title?: string | { absolute: string } | null;
};
function resolveTitle(
metadata: GeneratorInputMetadata,
options: { titleTemplate?: { default: string; template: string } },
) {
let title: string | null | undefined;
if (
metadata.title &&
typeof metadata.title === "object" &&
"absolute" in metadata.title
) {
title = metadata.title.absolute;
} else {
const { titleTemplate } = options;
if (!titleTemplate) {
title = metadata.title;
} else {
if (typeof metadata.title === "string") {
title = titleTemplate.template.split("%s").join(metadata.title);
} else {
title = titleTemplate.default;
}
}
}
return title;
}
export function createMetadataGenerator(
options: { titleTemplate?: { default: string; template: string } } = {},
) {
return (metadata: GeneratorInputMetadata) => {
const title = resolveTitle(metadata, options);
return generateMetadata({
...metadata,
title,
});
};
}
export type { InputMetadata, OutputMetadata, GeneratorInputMetadata };