astrocms 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +186 -0
- package/backend/parsers/components.ts +4 -4
- package/backend/parsers/frontmatter.ts +8 -28
- package/dist/_cms/{index-BdChuKfE.js → index-09wp6zYx.js} +1 -1
- package/dist/_cms/{index-7Pc6H4sv.js → index-6JDMRjpb.js} +1 -1
- package/dist/_cms/{index-C2vDuCBy.js → index-BLbwxB-T.js} +1 -1
- package/dist/_cms/{index-M4bKZFGh.js → index-BoSz7U0Z.js} +1 -1
- package/dist/_cms/{index-DM5GktYD.js → index-CFkco-F_.js} +1 -1
- package/dist/_cms/{index-Bpfn86Of.js → index-CGs7A6M0.js} +1 -1
- package/dist/_cms/{index-CoIA6zkf.js → index-CNodmjTW.js} +1 -1
- package/dist/_cms/{index-enNvjuOI.js → index-C_paHaVu.js} +1 -1
- package/dist/_cms/{index-Cdwq3Ev5.js → index-CelDrkwY.js} +1 -1
- package/dist/_cms/{index-DQf5bTbe.js → index-CfQ-lz7B.js} +1 -1
- package/dist/_cms/{index-9o-llXDY.js → index-Ckxjw6_V.js} +114 -114
- package/dist/_cms/{index-893Rzaov.js → index-ClAgPazm.js} +1 -1
- package/dist/_cms/{index-NwHME3i3.js → index-D1EUeT6H.js} +1 -1
- package/dist/_cms/{index-wHleENT0.js → index-DG4QoCve.js} +1 -1
- package/dist/_cms/{index-Bvz8WTJL.js → index-D_CH-VK7.js} +1 -1
- package/dist/_cms/{index-Ce64khPN.js → index-NEadZbXX.js} +1 -1
- package/dist/_cms/{index-BdC39RoS.js → index-QtwSDjQm.js} +1 -1
- package/dist/_cms/{index-DAAZmIPw.js → index-Qu04TL5H.js} +1 -1
- package/dist/index.html +1 -1
- package/package.json +5 -2
- package/shared/frontmatter.ts +78 -0
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import YAML from 'yaml'
|
|
2
|
+
|
|
3
|
+
export type FrontmatterData = Record<string, unknown>
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Matches an opening `---` fence, the YAML block, and the closing `---`.
|
|
7
|
+
* Leading whitespace and newlines before the fence are tolerated so that
|
|
8
|
+
* files authored with a blank line on top still parse correctly.
|
|
9
|
+
*/
|
|
10
|
+
export const FRONTMATTER_RE = /^\s*---\r?\n([\s\S]*?)\r?\n---/
|
|
11
|
+
|
|
12
|
+
/** Extract the raw YAML block from a markdown/MDX document, if any. */
|
|
13
|
+
export function extractRawFrontmatter(content: string): string | undefined {
|
|
14
|
+
const match = content.match(FRONTMATTER_RE)
|
|
15
|
+
return match ? match[1] : undefined
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Return the document body with the frontmatter block (and its trailing newline) stripped. */
|
|
19
|
+
export function extractBody(content: string): string {
|
|
20
|
+
return content.replace(FRONTMATTER_RE, '').replace(/^\r?\n/, '')
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Parse a YAML frontmatter string into a plain object. Invalid / non-object → empty. */
|
|
24
|
+
export function parseFrontmatterYaml(yaml: string): FrontmatterData {
|
|
25
|
+
const result = YAML.parse(yaml)
|
|
26
|
+
return result && typeof result === 'object' ? result : {}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** Serialize frontmatter data to YAML. Empty scalars and empty arrays are dropped. */
|
|
30
|
+
export function serializeFrontmatterYaml(data: FrontmatterData): string {
|
|
31
|
+
const filtered: FrontmatterData = {}
|
|
32
|
+
for (const [key, value] of Object.entries(data)) {
|
|
33
|
+
if (value === '' || value === undefined || value === null) continue
|
|
34
|
+
if (Array.isArray(value) && value.length === 0) continue
|
|
35
|
+
filtered[key] = value
|
|
36
|
+
}
|
|
37
|
+
if (Object.keys(filtered).length === 0) return ''
|
|
38
|
+
return YAML.stringify(filtered, { lineWidth: 0 }).trimEnd()
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Combine frontmatter data and body into a full markdown document. */
|
|
42
|
+
export function combineFrontmatterAndBody(
|
|
43
|
+
data: FrontmatterData,
|
|
44
|
+
body: string
|
|
45
|
+
): string {
|
|
46
|
+
const yaml = serializeFrontmatterYaml(data)
|
|
47
|
+
if (!yaml) return body
|
|
48
|
+
return `---\n${yaml}\n---\n\n${body}`
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Parse frontmatter from a supported content file's raw contents.
|
|
53
|
+
* - `.md` / `.mdx`: extract the YAML block and parse it.
|
|
54
|
+
* - `.yaml` / `.yml`: parse the whole document as YAML.
|
|
55
|
+
* - `.json`: parse the whole document as JSON.
|
|
56
|
+
* Any parse failure returns an empty object.
|
|
57
|
+
*/
|
|
58
|
+
export function parseFileFrontmatter(
|
|
59
|
+
content: string,
|
|
60
|
+
ext: string
|
|
61
|
+
): FrontmatterData {
|
|
62
|
+
try {
|
|
63
|
+
if (ext === '.md' || ext === '.mdx') {
|
|
64
|
+
const yaml = extractRawFrontmatter(content)
|
|
65
|
+
return yaml ? parseFrontmatterYaml(yaml) : {}
|
|
66
|
+
}
|
|
67
|
+
if (ext === '.yaml' || ext === '.yml') {
|
|
68
|
+
return parseFrontmatterYaml(content)
|
|
69
|
+
}
|
|
70
|
+
if (ext === '.json') {
|
|
71
|
+
const parsed = JSON.parse(content)
|
|
72
|
+
return parsed && typeof parsed === 'object' ? parsed : {}
|
|
73
|
+
}
|
|
74
|
+
} catch {
|
|
75
|
+
return {}
|
|
76
|
+
}
|
|
77
|
+
return {}
|
|
78
|
+
}
|