|
1 | 1 | // import type { ParsedContentFile } from '@nuxt/content' |
2 | | -import { parseMarkdown, stringifyMarkdown } from '@nuxtjs/mdc/runtime' |
3 | | -import { parseFrontMatter, stringifyFrontMatter } from 'remark-mdc' |
| 2 | +import { stringifyMarkdown } from '@nuxtjs/mdc/runtime' |
| 3 | +import { stringifyFrontMatter } from 'remark-mdc' |
4 | 4 | import type { MDCElement, MDCRoot } from '@nuxtjs/mdc' |
5 | 5 | import { type DatabasePageItem, type DatabaseItem, ContentFileExtension } from '../types' |
6 | 6 | import { omit, pick } from './object' |
7 | | -import { compressTree, decompressTree } from '@nuxt/content/runtime' |
| 7 | +import { decompressTree } from '@nuxt/content/runtime' |
8 | 8 | import { visit } from 'unist-util-visit' |
9 | 9 | import type { Node } from 'unist' |
10 | | -import type { MarkdownRoot } from '@nuxt/content' |
11 | | -import { destr } from 'destr' |
12 | 10 | import { getFileExtension } from './file' |
13 | 11 |
|
14 | 12 | const reservedKeys = ['id', 'fsPath', 'stem', 'extension', '__hash__', 'path', 'body', 'meta', 'rawbody'] |
15 | 13 |
|
16 | | -export function generateStemFromId(id: string) { |
17 | | - return id.split('/').slice(1).join('/').split('.').slice(0, -1).join('.') |
18 | | -} |
19 | | - |
20 | 14 | export function pickReservedKeysFromDocument(document: DatabaseItem) { |
21 | 15 | return pick(document, reservedKeys) |
22 | 16 | } |
@@ -69,102 +63,6 @@ export function isEqual(content1: string | null, content2: string | null): boole |
69 | 63 | return false |
70 | 64 | } |
71 | 65 |
|
72 | | -export async function generateDocumentFromContent(id: string, content: string): Promise<DatabaseItem | null> { |
73 | | - const [_id, _hash] = id.split('#') |
74 | | - const extension = getFileExtension(id) |
75 | | - |
76 | | - if (extension === ContentFileExtension.Markdown) { |
77 | | - return await generateDocumentFromMarkdownContent(id, content) |
78 | | - } |
79 | | - |
80 | | - if (extension === ContentFileExtension.YAML || extension === ContentFileExtension.YML) { |
81 | | - return await generateDocumentFromYAMLContent(id, content) |
82 | | - } |
83 | | - |
84 | | - if (extension === ContentFileExtension.JSON) { |
85 | | - return await generateDocumentFromJSONContent(id, content) |
86 | | - } |
87 | | - |
88 | | - return null |
89 | | -} |
90 | | - |
91 | | -export async function generateDocumentFromYAMLContent(id: string, content: string): Promise<DatabaseItem> { |
92 | | - const { data } = parseFrontMatter(`---\n${content}\n---`) |
93 | | - |
94 | | - // Keep array contents under `body` key |
95 | | - let parsed = data |
96 | | - if (Array.isArray(data)) { |
97 | | - console.warn(`YAML array is not supported in ${id}, moving the array into the \`body\` key`) |
98 | | - parsed = { body: data } |
99 | | - } |
100 | | - |
101 | | - return { |
102 | | - id, |
103 | | - extension: getFileExtension(id), |
104 | | - stem: generateStemFromId(id), |
105 | | - meta: {}, |
106 | | - ...parsed, |
107 | | - body: parsed.body || parsed, |
108 | | - } as never as DatabaseItem |
109 | | -} |
110 | | - |
111 | | -export async function generateDocumentFromJSONContent(id: string, content: string): Promise<DatabaseItem> { |
112 | | - let parsed: Record<string, unknown> = destr(content) |
113 | | - |
114 | | - // Keep array contents under `body` key |
115 | | - if (Array.isArray(parsed)) { |
116 | | - console.warn(`JSON array is not supported in ${id}, moving the array into the \`body\` key`) |
117 | | - parsed = { |
118 | | - body: parsed, |
119 | | - } |
120 | | - } |
121 | | - |
122 | | - // fsPath will be overridden by host |
123 | | - return { |
124 | | - id, |
125 | | - extension: ContentFileExtension.JSON, |
126 | | - stem: generateStemFromId(id), |
127 | | - meta: {}, |
128 | | - ...parsed, |
129 | | - body: parsed.body || parsed, |
130 | | - } as never as DatabaseItem |
131 | | -} |
132 | | - |
133 | | -export async function generateDocumentFromMarkdownContent(id: string, content: string): Promise<DatabaseItem> { |
134 | | - const document = await parseMarkdown(content, { |
135 | | - remark: { |
136 | | - plugins: { |
137 | | - 'remark-mdc': { |
138 | | - options: { |
139 | | - autoUnwrap: true, |
140 | | - }, |
141 | | - }, |
142 | | - }, |
143 | | - }, |
144 | | - }) |
145 | | - |
146 | | - // Remove nofollow from links |
147 | | - visit(document.body, (node: Node) => (node as MDCElement).type === 'element' && (node as MDCElement).tag === 'a', (node: Node) => { |
148 | | - if ((node as MDCElement).props?.rel?.join(' ') === 'nofollow') { |
149 | | - Reflect.deleteProperty((node as MDCElement).props!, 'rel') |
150 | | - } |
151 | | - }) |
152 | | - |
153 | | - const body = document.body.type === 'root' ? compressTree(document.body) : document.body as never as MarkdownRoot |
154 | | - |
155 | | - return { |
156 | | - id, |
157 | | - meta: {}, |
158 | | - extension: ContentFileExtension.Markdown, |
159 | | - stem: generateStemFromId(id), |
160 | | - body: { |
161 | | - ...body, |
162 | | - toc: document.toc, |
163 | | - }, |
164 | | - ...document.data, |
165 | | - } as never as DatabaseItem |
166 | | -} |
167 | | - |
168 | 66 | export async function generateContentFromDocument(document: DatabaseItem): Promise<string | null> { |
169 | 67 | const [id, _hash] = document.id.split('#') |
170 | 68 | const extension = getFileExtension(id) |
|
0 commit comments