Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🧹 Upgrade unified/unist stack #1603

Draft
wants to merge 51 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
639e0c6
chore: bump unist- utilities
agoose77 Oct 25, 2024
132898b
chore: move to vfile 6
agoose77 Oct 28, 2024
2604e13
chore: update vfile-reporter
agoose77 Oct 28, 2024
3ec15ac
chore: upgrade unist-builder
agoose77 Oct 28, 2024
aeb84ba
chore: upgrade unified
agoose77 Oct 28, 2024
08a88a3
chore: upgrade rehype
agoose77 Oct 28, 2024
c994afd
chore: bump mdast types
agoose77 Oct 28, 2024
0779eea
chore: bump "unist-util-modify-children"
agoose77 Oct 28, 2024
99f3a8b
chore: bump unist-util-remove
agoose77 Oct 28, 2024
daea7e9
chore: bump types/hast
agoose77 Oct 28, 2024
7893ca8
chore: bump unist-util-map
agoose77 Oct 28, 2024
b4ef06e
chore: bump unist-builder
agoose77 Oct 28, 2024
c0eebdd
chore: bump mdast-util-find-and-replace
agoose77 Oct 28, 2024
3a59451
chore: bump hast-util-to-mdast
agoose77 Oct 28, 2024
6683af7
chore: bump hast-util-to-mdast
agoose77 Oct 28, 2024
4f1a76c
chore: correct rehype-remark
agoose77 Oct 28, 2024
e9d57c5
chore: bump mdast-util-gfm*
agoose77 Oct 28, 2024
1406e8e
chore: drop mdast and hast packages
agoose77 Oct 28, 2024
7b7c063
chore: update rehype packages
agoose77 Oct 28, 2024
1d16fca
chore: bump mdast-util-to-markdown
agoose77 Oct 28, 2024
2c825b4
chore: bump vfile-message
agoose77 Oct 28, 2024
4480167
fix: for now, assume typings are wrong
agoose77 Oct 28, 2024
f8c4ae6
fix: type hints for compilers
agoose77 Oct 28, 2024
7d39be6
wip: type fixes
agoose77 Oct 28, 2024
f88574b
fix: select cast to GenericNode
agoose77 Oct 29, 2024
bb438eb
fix: update handlers
agoose77 Oct 29, 2024
ee39035
fix: update compiler usage
agoose77 Oct 29, 2024
e791c85
fix: cast select result to GenericNode
agoose77 Oct 29, 2024
2e1b9bb
fix: more types
agoose77 Oct 29, 2024
2bc14b1
fix: loosen casting between mdast and genericnode
agoose77 Oct 29, 2024
b3bfb1e
chore: bump remark-parse
agoose77 Oct 29, 2024
6b647d9
fix: loosen casting between mdast and genericnode
agoose77 Oct 29, 2024
d57d6da
fix: handler typing
agoose77 Oct 29, 2024
f2fc139
fix: loosen casting between mdast and genericnode
agoose77 Oct 29, 2024
8e91406
fix: loosen casting between mdast and genericnode
agoose77 Oct 29, 2024
be5670c
fix: loosen casting between mdast and genericnode
agoose77 Oct 29, 2024
ba4e796
fix: updates to typing in vfile-message
agoose77 Oct 29, 2024
02ea4ba
wip: pin turbo for now
agoose77 Oct 29, 2024
2dafdd2
chore: bump lockfile
agoose77 Nov 5, 2024
637b402
fix: use of h instead of u
agoose77 Nov 5, 2024
d84a715
test: fix test cases
agoose77 Nov 5, 2024
fb08fbb
test: fix test case
agoose77 Nov 5, 2024
92efde8
wip: change call to remote
agoose77 Nov 5, 2024
d6b3cfd
wip: drop return dependency of remove
agoose77 Nov 5, 2024
3c5f748
fix: preserve parents in getFrontmatter
agoose77 Nov 5, 2024
afd8530
fix: patch node
agoose77 Nov 6, 2024
a5d347d
test: skip footnotes for now
agoose77 Nov 6, 2024
38326a9
chore: run lint
agoose77 Nov 6, 2024
eda1945
chore: run prettier
agoose77 Nov 6, 2024
6ec763a
chore: more appeasement
agoose77 Nov 6, 2024
42c0123
fix: fixes for escaping
agoose77 Nov 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10,491 changes: 6,290 additions & 4,201 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"npm-run-all": "^4.1.5",
"prettier": "latest",
"rimraf": "^5.0.1",
"turbo": "latest",
"turbo": "^1.10.0",
"typescript": "^5.6.2",
"vitest": "^2.1.2"
},
Expand Down
12 changes: 6 additions & 6 deletions packages/jats-to-myst/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@
"myst-spec": "^0.0.5",
"myst-spec-ext": "^1.7.2",
"myst-transforms": "^1.3.26",
"unified": "^10.0.0",
"unist-builder": "^3.0.0",
"unist-util-select": "^4.0.3",
"unist-util-remove": "^3.1.0",
"vfile": "^5.0.0",
"vfile-reporter": "^7.0.4"
"unified": "^11.0.0",
"unist-builder": "^4.0.0",
"unist-util-select": "^5.0.0",
"unist-util-remove": "^4.0.0",
"vfile": "^6.0.0",
"vfile-reporter": "^8.0.0"
},
"devDependencies": {
"myst-to-tex": "^1.0.38"
Expand Down
40 changes: 22 additions & 18 deletions packages/jats-to-myst/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ const handlers: Record<string, Handler> = {
state.openNode('tabSet');
node.children?.forEach((n) => {
state.openNode('tabItem', {
title: toText(select('label', n)),
sync: toText(select('label', n)),
title: toText(select('label', n) as GenericNode),
sync: toText(select('label', n) as GenericNode),
});
state.renderChildren({ children: [n] });
state.closeNode();
Expand Down Expand Up @@ -451,18 +451,23 @@ export class JatsParser implements IJatsParser {
return this.pushNode(node);
}
}
declare module 'unified' {
interface CompileResultMap {
VFile: VFile;
}
}

export const jatsToMystPlugin: Plugin<[Jats, Options?], Root, Root> = function (jats, opts) {
this.Compiler = (node: GenericParent, file: VFile) => {
this.compiler = (node, file: VFile) => {
const tree = jats.abstract
? {
type: 'root',
children: [
u('block', { part: 'abstract' }, copyNode(jats.abstract).children),
...copyNode(node).children,
...copyNode(node as any).children,
],
}
: copyNode(node);
: copyNode(node as any);
// Can do better than this in the future, but for now, just put them at the end!
const floatsGroup = selectAll('floats-group', jats.tree) as GenericParent[];
if (floatsGroup.length > 0) {
Expand All @@ -479,18 +484,21 @@ export const jatsToMystPlugin: Plugin<[Jats, Options?], Root, Root> = function (
jats.references.map((bibr) => {
const id = bibr.id;
const names = selectAll('name,string-name', bibr)
.map((n) => `${toText(select('surname', n))}, ${toText(select('given-names', n))}`)
.map(
(n) =>
`${toText(select('surname', n) as GenericNode)}, ${toText(select('given-names', n) as GenericNode)}`,
)
.join(', ');
const year = toText(select('year', bibr));
const title = toText(select('article-title', bibr));
const source = toText(select('source', bibr));
const volume = toText(select('volume', bibr));
const fpage = toText(select('fpage', bibr));
const lpage = toText(select('lpage', bibr));
const year = toText(select('year', bibr) as GenericNode);
const title = toText(select('article-title', bibr) as GenericNode);
const source = toText(select('source', bibr) as GenericNode);
const volume = toText(select('volume', bibr) as GenericNode);
const fpage = toText(select('fpage', bibr) as GenericNode);
const lpage = toText(select('lpage', bibr) as GenericNode);
const doiElement = selectAll('ext-link,[pub-id-type=doi]', bibr).find((e) =>
doi.validate(toText(e)),
doi.validate(toText(e as GenericNode)),
);
const doiString = doiElement ? toText(doiElement) : undefined;
const doiString = doiElement ? toText(doiElement as GenericNode) : undefined;
const doiLink = doiString ? ` <a href=${doi.buildUrl(doiString)}>${doiString}</a>` : '';
return [
id,
Expand Down Expand Up @@ -520,10 +528,6 @@ export const jatsToMystPlugin: Plugin<[Jats, Options?], Root, Root> = function (
file.result = result;
return file;
};

return (node: Root) => {
return node;
};
};

export function jatsToMystTransform(
Expand Down
2 changes: 1 addition & 1 deletion packages/markdown-it-myst/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@
"dependencies": {
"js-yaml": "^4.1.0",
"markdown-it": "^13.0.1",
"vfile": "^5.3.7"
"vfile": "^6.0.0"
}
}
8 changes: 4 additions & 4 deletions packages/myst-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,13 @@
"simple-validators": "^1.1.0",
"strip-ansi": "^7.0.1",
"tex-to-myst": "^1.0.40",
"unified": "^10.1.2",
"unified": "^11.0.0",
"unist-util-filter": "^4.0.0",
"unist-util-remove": "^3.1.0",
"unist-util-select": "^4.0.3",
"unist-util-remove": "^4.0.0",
"unist-util-select": "^5.0.0",
"unist-util-visit": "^4.0.0",
"uuid": "^8.3.2",
"vfile": "^5.3.5",
"vfile": "^6.0.0",
"which": "^3.0.1",
"ws": "^8.9.0",
"xml-js": "^1.6.11",
Expand Down
13 changes: 8 additions & 5 deletions packages/myst-cli/src/build/docx/single.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'node:fs';
import path from 'node:path';
import type { Content } from 'mdast';
import type { RootContent } from 'mdast';
import { createDocFromState, DocxSerializer, writeDocx } from 'myst-to-docx';
import { tic, writeFileToFolder } from 'myst-cli-utils';
import {
Expand All @@ -12,7 +12,7 @@ import {
import type { RendererDoc } from 'myst-templates';
import MystTemplate from 'myst-templates';
import { htmlTransform } from 'myst-transforms';
import { fileError, fileWarn, RuleId, TemplateKind } from 'myst-common';
import { fileError, fileWarn, RuleId, TemplateKind, type GenericNode } from 'myst-common';
import { selectAll } from 'unist-util-select';
import { filterKeys } from 'simple-validators';
import { VFile } from 'vfile';
Expand Down Expand Up @@ -40,7 +40,10 @@ function defaultWordRenderer(
vfile: VFile,
) {
const { mdast, frontmatter, references } = data;
const frontmatterNodes = createArticleTitle(frontmatter.title, frontmatter.authors) as Content[];
const frontmatterNodes = createArticleTitle(
frontmatter.title,
frontmatter.authors,
) as RootContent[];
const serializer = new DocxSerializer(
vfile,
{
Expand All @@ -54,7 +57,7 @@ function defaultWordRenderer(
frontmatter,
);
frontmatterNodes.forEach((node) => {
serializer.render(node);
serializer.render(node as GenericNode);
});
serializer.renderChildren(mdast);
const referencesDocStates = Object.values(references.cite?.data ?? {})
Expand All @@ -69,7 +72,7 @@ function defaultWordRenderer(
serializer.renderChildren(referencesRoot);
}
selectAll('footnoteDefinition', mdast).forEach((footnote) => {
serializer.render(footnote);
serializer.render(footnote as GenericNode);
});
const logo = path.join(staticPath, 'logo.png');
const docfooter = fs.existsSync(logo) && !opts.hideFooter ? createFooter(logo) : undefined;
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-cli/src/build/tex/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function mdastToTex(
...frontmatter.settings?.myst_to_tex,
});
const result = pipe.runSync(mdast as any);
const tex = pipe.stringify(result);
const tex = pipe.stringify(result as any);
logMessagesFromVFile(session, tex);
return tex.result as LatexResult;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-cli/src/build/typst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function mdastToTypst(
// ...frontmatter.settings?.myst_to_tex,
});
const result = pipe.runSync(mdast as any);
const typ = pipe.stringify(result);
const typ = pipe.stringify(result as any);
logMessagesFromVFile(session, typ);
return typ.result as TypstResult;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/myst-cli/src/executablePlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
type RoleSpec,
type TransformSpec,
type GenericNode,
type GenericParent,
} from 'myst-common';
import { spawn, spawnSync } from 'node:child_process';

Expand Down Expand Up @@ -117,7 +118,7 @@ function wrapTransform(
return {
...transform,
plugin: () => {
return async (node) => {
return async (node: GenericParent) => {
// Modify the tree in-place
const result = await executeAsyncParser(session, transform.name, node, path, 'transform');
Object.assign(node, result);
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-cli/src/frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function processPageFrontmatter(
const cache = castSession(session);
const state = session.store.getState();
const siteFrontmatter = selectors.selectCurrentSiteConfig(state) ?? {};
const projectFrontmatter = path ? selectors.selectLocalProjectConfig(state, path) ?? {} : {};
const projectFrontmatter = path ? (selectors.selectLocalProjectConfig(state, path) ?? {}) : {};

const frontmatter = fillPageFrontmatter(pageFrontmatter, projectFrontmatter, validationOpts);
const siteTemplate = cache.$siteTemplate;
Expand Down
2 changes: 1 addition & 1 deletion packages/myst-cli/src/process/mdast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ import { rawDirectiveTransform } from '../transforms/raw.js';

const LINKS_SELECTOR = 'link,card,linkBlock';

const pluginUtils: PluginUtils = { select, selectAll };
const pluginUtils: PluginUtils = { select, selectAll } as any;

const htmlHandlers = {
comment(h: any, node: any) {
Expand Down
4 changes: 2 additions & 2 deletions packages/myst-cli/src/process/site.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { basename, extname, join } from 'node:path';
import chalk from 'chalk';
import { Inventory, Domains } from 'intersphinx';
import { writeFileToFolder, tic, hashAndCopyStaticFile } from 'myst-cli-utils';
import { RuleId, toText, plural, slugToUrl } from 'myst-common';
import { RuleId, toText, plural, slugToUrl, type GenericNode } from 'myst-common';
import type { SiteConfig, SiteProject } from 'myst-config';
import type { Node } from 'myst-spec';
import type { SearchRecord, MystSearchIndex } from 'myst-spec-ext';
Expand Down Expand Up @@ -110,7 +110,7 @@ function getReferenceTitleAsText(targetNode: Node): string | undefined {
return toText(targetNode);
}
const caption = select('caption > paragraph', targetNode);
if (caption) return toText(caption);
if (caption) return toText(caption as GenericNode);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/myst-cli/src/store/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { VFileMessage } from 'vfile-message';
import type { Position } from 'unist';

export type ExternalLinkResult = {
url: string;
Expand All @@ -15,7 +15,7 @@ export type BuildWarning = {
kind: WarningKind;
note?: string | null;
url?: string | null;
position?: VFileMessage['position'];
position?: Position;
ruleId?: string | null;
};

Expand Down
1 change: 1 addition & 0 deletions packages/myst-cli/src/transforms/crossReferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export async function transformMystXRefs(
} else {
const data = await fetchMystXRefData(session, node as CrossReference, vfile);
if (!data) return;
console.debug({ identifier: node.identifier });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove!

const targets = nodesFromMystXRefData(data, node.identifier, vfile, {
urlSource: node.urlSource,
maxNodes: 1,
Expand Down
4 changes: 2 additions & 2 deletions packages/myst-cli/src/transforms/images.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { computeHash, hashAndCopyStaticFile, isUrl } from 'myst-cli-utils';
import { remove } from 'unist-util-remove';
import { selectAll } from 'unist-util-select';
import path from 'node:path';
import type { VFileMessage } from 'vfile-message';
import type { Position } from 'unist';
import type { PageFrontmatter } from 'myst-frontmatter';
import type { Image } from 'myst-spec-ext';
import { extFromMimeType } from 'nbtx';
Expand Down Expand Up @@ -127,7 +127,7 @@ export async function saveImageInStaticFolder(
urlSource: string,
sourceFile: string,
writeFolder: string,
opts?: { altOutputFolder?: string; position?: VFileMessage['position'] },
opts?: { altOutputFolder?: string; position?: Position },
): Promise<{ urlSource: string; url: string } | null> {
const sourceFileFolder = path.dirname(sourceFile);
const imageLocalFile = path.join(sourceFileFolder, urlSource);
Expand Down
4 changes: 2 additions & 2 deletions packages/myst-cli/src/utils/addWarningForFile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chalk from 'chalk';
import type { VFileMessage } from 'vfile-message';
import type { Position } from 'unist';
import type { ISession } from '../session/types.js';
import { warnings } from '../store/reducers.js';
import type { WarningKind } from '../store/types.js';
Expand All @@ -13,7 +13,7 @@ export function addWarningForFile(
opts?: {
note?: string | null;
url?: string | null;
position?: VFileMessage['position'];
position?: Position;
ruleId?: string | null;
/** This key can be combined with the ruleId to suppress a warning */
key?: string | null;
Expand Down
7 changes: 6 additions & 1 deletion packages/myst-cli/src/utils/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ export function logMessagesFromVFile(session: ISession, file?: VFile): void {
const kind: WarningKind =
message.fatal === null ? 'info' : message.fatal === false ? 'warn' : 'error';
addWarningForFile(session, file.path, message.message, kind, {
position: message.position,
// TODO: does this logic make sense, or should we support `place`?
position: message.place
? 'start' in message.place
? message.place
: { start: message.place, end: message.place }
: undefined,
note: message.note,
url: message.url,
ruleId: message.ruleId,
Expand Down
18 changes: 10 additions & 8 deletions packages/myst-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,23 @@
"build": "npm-run-all -l clean -p build:esm"
},
"dependencies": {
"css.escape": "^1.5.1",
"mdast": "^3.0.0",
"myst-frontmatter": "^1.7.4",
"myst-spec": "^0.0.5",
"nanoid": "^4.0.0",
"unified": "^10.1.2",
"unist-util-remove": "^3.1.0",
"unist-util-select": "^4.0.3",
"unified": "^11.0.0",
"unist-util-remove": "^4.0.0",
"unist-util-select": "^5.0.0",
"unist-util-visit": "^4.1.2",
"vfile": "^5.0.0",
"vfile-message": "^3.0.0"
"vfile": "^6.0.0",
"vfile-message": "^4.0.0"
},
"devDependencies": {
"myst-spec-ext": "^1.7.4",
"unist-builder": "3.0.0",
"@jupyterlab/nbformat": "^3.5.2",
"@lumino/coreutils": "^2.0.0"
"@lumino/coreutils": "^2.0.0",
"@types/css.escape": "^1.5.2",
"myst-spec-ext": "^1.7.4",
"unist-builder": "4.0.0"
}
}
14 changes: 3 additions & 11 deletions packages/myst-common/src/extractParts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function coercePart(part?: string | string[]): string[] {
export function selectBlockParts(tree: GenericParent, part?: string | string[]): Block[] {
const parts = coercePart(part);
if (parts.length === 0) return [];
const blockParts = selectAll('block', tree).filter((block) => {
const blockParts = (selectAll('block', tree) as Block[]).filter((block) => {
const blockTags: string[] = (
block.data?.tags && Array.isArray(block.data.tags) ? block.data.tags : []
).map((tag) => tag?.toLowerCase());
Expand Down Expand Up @@ -84,14 +84,6 @@ function createPartBlock(
return block;
}

function forcedRemove(tree: GenericParent, test: string) {
let success = remove(tree, test);
if (!success) {
success = remove(tree, { cascade: false }, test);
}
return success;
}

/**
* Extract implicit part based on heading name
*
Expand Down Expand Up @@ -150,7 +142,7 @@ export function extractImplicitPart(
});
if (blockParts.length === 0) return;
const partsTree = { type: 'root', children: blockParts } as GenericParent;
forcedRemove(tree, '__part_delete__');
remove(tree, '__part_delete__');
return partsTree;
}

Expand Down Expand Up @@ -209,6 +201,6 @@ export function extractPart(
blockParts.forEach((block) => {
(block as any).type = '__delete__';
});
forcedRemove(tree, '__delete__');
remove(tree, '__delete__');
return partsTree;
}
Loading
Loading