Skip to content

Commit fad0b0f

Browse files
committed
Update notes to specific version.
1 parent e6608fe commit fad0b0f

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

shared/links-metadata/src/link.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { SynctexStore } from "./SynctexStore";
22
import type { TexStore } from "./TexStore";
3-
import { type Metadata, ORIGIN } from "./metadata";
3+
import { type Metadata, ORIGIN, shortVersionId } from "./metadata";
44
import { migrateSelection } from "./migrate";
55
import type { ISynctexBlockId } from "./types";
66

@@ -61,6 +61,7 @@ export async function parseAndMigrateLink(
6161
meta: Metadata,
6262
synctexStore: SynctexStore,
6363
texStore: TexStore,
64+
toVersion: string,
6465
lineNumber = 0,
6566
): Promise<Link> {
6667
const linkData = parseLink(url, meta);
@@ -76,30 +77,31 @@ export async function parseAndMigrateLink(
7677
};
7778
}
7879
const { version, versionName, selectionStart, selectionEnd } = linkData;
79-
const isOutdated = version !== meta.metadata.latest;
80+
const isOutdated = version !== toVersion;
8081
let updated: string | null = null;
8182
let migrated = false;
8283

83-
// check if the blocks are still there in the latest metadata
84+
// check if the blocks are still there in the requested version of metadata
8485
if (isOutdated && selectionStart && selectionEnd) {
8586
const migratedSelection = await migrateSelection(
8687
{ selectionStart, selectionEnd },
8788
version,
88-
meta.metadata.latest,
89+
toVersion,
8990
synctexStore,
9091
texStore,
9192
);
9293

94+
const toVersionShort = shortVersionId(toVersion);
9395
if (migratedSelection) {
94-
updated = `${ORIGIN}#/${meta.latestShort}/${encodePageNumberAndIndex(migratedSelection.selectionStart)}${encodePageNumberAndIndex(migratedSelection.selectionEnd)}`;
96+
updated = `${ORIGIN}#/${toVersionShort}/${encodePageNumberAndIndex(migratedSelection.selectionStart)}${encodePageNumberAndIndex(migratedSelection.selectionEnd)}`;
9597
migrated = true;
9698
} else {
97-
const latestSynctex = await synctexStore.getSynctex(meta.metadata.latest);
99+
const latestSynctex = await synctexStore.getSynctex(toVersion);
98100
const hasStart = !!latestSynctex.blocksByPage.get(selectionStart.pageNumber)?.[selectionStart.index];
99101
const hasEnd = !!latestSynctex.blocksByPage.get(selectionEnd.pageNumber)?.[selectionEnd.index];
100102

101103
if (hasStart && hasEnd) {
102-
updated = `${ORIGIN}#/${meta.latestShort}/${linkData.blocks}`;
104+
updated = `${ORIGIN}#/${toVersionShort}/${linkData.blocks}`;
103105
}
104106
}
105107
}

shared/links-metadata/src/metadata.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function getShortVersionMapping(data: JsonMetadata) {
6363
return res;
6464
}
6565

66-
function shortVersionId(hash: string) {
66+
export function shortVersionId(hash: string) {
6767
const SHORT_COMMIT_HASH_LENGTH = 7; // as many as git uses for `git rev-parse --short`
6868
return hash.substring(0, SHORT_COMMIT_HASH_LENGTH);
6969
}

tools/links-check/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ async function main() {
2121
"--ignore-file <path>",
2222
"Path to a file containing patterns to ignore. Gitignore format applies. Patterns are resolved according to current working directory.",
2323
)
24+
.option("--version <name>", "Commit hash of specific version to update to (instead of latest)")
2425
.option("--write", "Perform link migrations in provided source files.")
2526
.option("--fix", "Alias for --write.")
2627
.action(async (paths, options) => {
@@ -62,7 +63,7 @@ async function main() {
6263
console.time(label);
6364
let report: Report | null = null;
6465
try {
65-
report = await scan(files, metadata);
66+
report = await scan(files, metadata, options.version);
6667
} finally {
6768
console.timeEnd(label);
6869
}

tools/links-check/scan.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,27 @@ class Timer {
2323
}
2424
}
2525

26-
export async function scan(files: Path[], metadata: Metadata): Promise<Report> {
26+
export async function scan(files: Path[], metadata: Metadata, version?: string): Promise<Report> {
2727
const timer = new Timer();
2828
const synctexStore = new SynctexStore();
2929
const texStore = new TexStore();
3030
const cwd = process.cwd();
31+
const versionData = findVersion(metadata, version);
32+
const toVersion = versionData.hash;
33+
3134
const results = await Promise.allSettled(
3235
files.map(async (file) => {
3336
const relativeFilePath = path.relative(cwd, file);
3437
timer.start(relativeFilePath);
35-
const fileReport = await scanFile(file, metadata, synctexStore, texStore);
38+
const fileReport = await scanFile(file, metadata, synctexStore, texStore, toVersion);
3639
timer.end(relativeFilePath, fileReport.allLinks.length > 0);
3740
printFileReport(fileReport);
3841
return fileReport;
3942
}),
4043
);
4144

4245
const report = {
43-
latestVersion: metadata.metadata.versions[metadata.metadata.latest]?.name || metadata.latestShort,
46+
latestVersion: versionData.name || version || metadata.latestShort,
4447
detected: new Map(),
4548
outdated: new Map(),
4649
failed: new Map(),
@@ -63,11 +66,29 @@ export async function scan(files: Path[], metadata: Metadata): Promise<Report> {
6366
return Promise.resolve(report);
6467
}
6568

69+
function findVersion(metadata: Metadata, version?: string) {
70+
const v = version === "latest" ? metadata.metadata.latest : version;
71+
const versionData = metadata.metadata.versions[v || metadata.metadata.latest];
72+
if (versionData) {
73+
return versionData;
74+
}
75+
76+
// try lookup by name instead.
77+
for (const v of Object.values(metadata.metadata.versions)) {
78+
if (v.name === version) {
79+
return v;
80+
}
81+
}
82+
83+
throw new Error(`Version ${version} not found.`);
84+
}
85+
6686
async function scanFile(
6787
path: Path,
6888
metadata: Metadata,
6989
synctexStore: SynctexStore,
7090
texStore: TexStore,
91+
toVersion: string,
7192
): Promise<FileReport> {
7293
const links: [number, string][] = [];
7394
const report: FileReport = {
@@ -83,7 +104,9 @@ async function scanFile(
83104
});
84105

85106
const linksParsed = await Promise.all(
86-
links.map(([lineNumber, link]) => parseAndMigrateLink(link, metadata, synctexStore, texStore, lineNumber)),
107+
links.map(([lineNumber, link]) =>
108+
parseAndMigrateLink(link, metadata, synctexStore, texStore, toVersion, lineNumber),
109+
),
87110
);
88111

89112
report.allLinks = linksParsed;

0 commit comments

Comments
 (0)