-
Notifications
You must be signed in to change notification settings - Fork 50
/
mdast.ts
269 lines (259 loc) · 8.82 KB
/
mdast.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import path from 'path';
import type { Root } from 'mdast';
import { tic } from 'myst-cli-utils';
import type { References } from 'myst-common';
import { SourceFileKind } from 'myst-common';
import type { LinkTransformer } from 'myst-transforms';
import {
basicTransformationsPlugin,
htmlPlugin,
footnotesPlugin,
ReferenceState,
MultiPageReferenceState,
resolveReferencesTransform,
mathPlugin,
codePlugin,
enumerateTargetsPlugin,
keysTransform,
linksTransform,
MystTransformer,
WikiTransformer,
GithubTransformer,
RRIDTransformer,
DOITransformer,
joinGatesPlugin,
} from 'myst-transforms';
import { unified } from 'unified';
import { VFile } from 'vfile';
import { getPageFrontmatter, processPageFrontmatter } from '../frontmatter';
import { selectors } from '../store';
import { watch } from '../store/reducers';
import type { ISession } from '../session/types';
import { castSession } from '../session';
import type { RendererData } from '../transforms/types';
import {
checkLinksTransform,
embedDirective,
importMdastFromJson,
includeFilesDirective,
liftCodeMetadataToBlock,
transformLinkedDOIs,
transformOutputs,
transformCitations,
transformImages,
transformImageFormats,
transformThumbnail,
StaticFileTransformer,
inlineExpressionsPlugin,
} from '../transforms';
import type { ImageExtensions } from '../utils';
import { logMessagesFromVFile } from '../utils';
import { combineCitationRenderers } from './citations';
import { bibFilesInDir, selectFile } from './file';
import { loadIntersphinx } from './intersphinx';
const LINKS_SELECTOR = 'link,card,linkBlock';
const htmlHandlers = {
comment(h: any, node: any) {
// Prevents HTML comments from showing up as text in web
// TODO: Remove once this is landed in mystjs
const result = h(node, 'comment');
(result as any).value = node.value;
return result;
},
};
export type PageReferenceStates = {
state: ReferenceState;
file: string;
url: string | null;
dataUrl: string | null;
}[];
export type TransformFn = (
session: ISession,
opts: Parameters<typeof transformMdast>[1],
) => Promise<void>;
export async function transformMdast(
session: ISession,
opts: {
file: string;
imageWriteFolder: string;
projectPath?: string;
projectSlug?: string;
pageSlug?: string;
imageAltOutputFolder?: string;
imageExtensions?: ImageExtensions[];
watchMode?: boolean;
extraTransforms?: TransformFn[];
minifyMaxCharacters?: number;
index?: string;
},
) {
const {
file,
imageWriteFolder,
projectPath,
pageSlug,
projectSlug,
imageAltOutputFolder,
imageExtensions,
extraTransforms,
watchMode = false,
minifyMaxCharacters,
index,
} = opts;
const toc = tic();
const { store, log } = session;
const cache = castSession(session);
if (!cache.$mdast[file]) return;
const { mdast: mdastPre, kind, frontmatter: preFrontmatter } = cache.$mdast[file].pre;
if (!mdastPre) throw new Error(`Expected mdast to be parsed for ${file}`);
log.debug(`Processing "${file}"`);
// Use structuredClone in future (available in node 17)
const mdast = JSON.parse(JSON.stringify(mdastPre)) as Root;
const frontmatter = preFrontmatter
? processPageFrontmatter(session, preFrontmatter, projectPath)
: getPageFrontmatter(session, mdast, file, projectPath);
const references: References = {
cite: { order: [], data: {} },
};
const vfile = new VFile(); // Collect errors on this file
vfile.path = file;
const state = new ReferenceState({ numbering: frontmatter.numbering, file: vfile });
cache.$internalReferences[file] = state;
// Import additional content from mdast or other files
importMdastFromJson(session, file, mdast);
includeFilesDirective(session, file, mdast);
// This needs to come before basic transformations since it may add labels to blocks
liftCodeMetadataToBlock(session, file, mdast);
await unified()
.use(basicTransformationsPlugin)
.use(inlineExpressionsPlugin) // Happens before math and images!
.use(htmlPlugin, { htmlHandlers })
.use(mathPlugin, { macros: frontmatter.math })
.use(enumerateTargetsPlugin, { state }) // This should be after math
.use(joinGatesPlugin)
.run(mdast, vfile);
// Run the link transformations that can be done without knowledge of other files
const intersphinx = projectPath ? await loadIntersphinx(session, { projectPath }) : [];
const transformers = [
new WikiTransformer(),
new GithubTransformer(),
new RRIDTransformer(),
new DOITransformer(), // This also is picked up in the next transform
new MystTransformer(intersphinx),
];
linksTransform(mdast, vfile, { transformers, selector: LINKS_SELECTOR });
// Initialize citation renderers for this (non-bib) file
cache.$citationRenderers[file] = await transformLinkedDOIs(log, mdast, cache.$doiRenderers, file);
const rendererFiles = [file];
if (projectPath) {
rendererFiles.unshift(projectPath);
} else {
const localFiles = (await bibFilesInDir(session, path.dirname(file))) || [];
rendererFiles.push(...localFiles);
}
// Combine file-specific citation renderers with project renderers from bib files
const fileCitationRenderer = combineCitationRenderers(cache, ...rendererFiles);
// Kind needs to still be Article here even if jupytext, to handle outputs correctly
await transformOutputs(session, mdast, kind, imageWriteFolder, {
altOutputFolder: imageAltOutputFolder,
minifyMaxCharacters,
});
transformCitations(log, mdast, fileCitationRenderer, references, file);
await unified()
.use(codePlugin, { lang: frontmatter?.kernelspec?.language })
.use(footnotesPlugin) // Needs to happen near the end
.run(mdast, vfile);
await transformImages(session, mdast, file, imageWriteFolder, {
altOutputFolder: imageAltOutputFolder,
imageExtensions,
});
// Must happen after transformImages
await transformImageFormats(session, mdast, file, imageWriteFolder, {
altOutputFolder: imageAltOutputFolder,
imageExtensions,
});
// Note, the thumbnail transform must be **after** images, as it may read the images
await transformThumbnail(session, mdast, file, frontmatter, imageWriteFolder, {
altOutputFolder: imageAltOutputFolder,
});
const sha256 = selectors.selectFileInfo(store.getState(), file).sha256 as string;
const useSlug = pageSlug !== index;
const url = projectSlug
? `/${projectSlug}/${useSlug ? pageSlug : ''}`
: `/${useSlug ? pageSlug : ''}`;
const dataUrl = projectSlug ? `/${projectSlug}/${pageSlug}.json` : `/${pageSlug}.json`;
store.dispatch(
watch.actions.updateFileInfo({
path: file,
title: frontmatter.title,
short_title: frontmatter.short_title,
description: frontmatter.description,
date: frontmatter.date,
thumbnail: frontmatter.thumbnail,
thumbnailOptimized: frontmatter.thumbnailOptimized,
tags: frontmatter.tags,
url,
dataUrl,
}),
);
const data: RendererData = {
kind: frontmatter.kernelspec || frontmatter.jupytext ? SourceFileKind.Notebook : kind,
file,
sha256,
slug: pageSlug,
dependencies: [],
frontmatter,
mdast,
references,
};
cache.$mdast[file].post = data;
if (extraTransforms) {
await Promise.all(
extraTransforms.map(async (transform) => {
await transform(session, opts);
}),
);
}
logMessagesFromVFile(session, vfile);
if (!watchMode) log.info(toc(`📖 Built ${file} in %s.`));
}
export async function postProcessMdast(
session: ISession,
{
file,
checkLinks,
pageReferenceStates,
extraLinkTransformers,
}: {
file: string;
checkLinks?: boolean;
pageReferenceStates?: PageReferenceStates;
extraLinkTransformers?: LinkTransformer[];
},
) {
const toc = tic();
const { log } = session;
const cache = castSession(session);
const mdastPost = selectFile(session, file);
if (!mdastPost) return;
const fileState = cache.$internalReferences[file];
const state = pageReferenceStates
? new MultiPageReferenceState(pageReferenceStates, file)
: fileState;
// NOTE: This is doing things in place, we should potentially make this a different state?
const transformers = [
...(extraLinkTransformers || []),
new StaticFileTransformer(session, file), // Links static files and internally linked files
];
linksTransform(mdastPost.mdast, state.file as VFile, {
transformers,
selector: LINKS_SELECTOR,
});
resolveReferencesTransform(mdastPost.mdast, state.file as VFile, { state });
embedDirective(session, mdastPost.mdast, mdastPost.dependencies, state);
// Ensure there are keys on every node after post processing
keysTransform(mdastPost.mdast);
logMessagesFromVFile(session, fileState.file);
log.debug(toc(`Transformed mdast cross references and links for "${file}" in %s`));
if (checkLinks) await checkLinksTransform(session, file, mdastPost.mdast);
}