Skip to content

Commit a21cfbb

Browse files
authored
Update notes to specific version. (#156)
* Update notes to specific version. * Address review comments. * Update to 'latest'.
1 parent 8abf8ba commit a21cfbb

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

shared/links-metadata/src/link.ts

Lines changed: 11 additions & 9 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 { ISelectionParams, 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);
@@ -78,30 +79,31 @@ export async function parseAndMigrateLink(
7879
};
7980
}
8081
const { version, versionName, selectionStart, selectionEnd } = linkData;
81-
const isOutdated = version !== meta.metadata.latest;
82+
const isOutdated = version !== toVersion;
8283
let updated: string | null = null;
8384
let migrated = false;
8485

85-
// check if the blocks are still there in the latest metadata
86+
// check if the blocks are still there in the requested version of metadata
8687
if (isOutdated && selectionStart && selectionEnd) {
8788
const migratedSelection = await migrateSelection(
8889
{ selectionStart, selectionEnd },
8990
version,
90-
meta.metadata.latest,
91+
toVersion,
9192
synctexStore,
9293
texStore,
9394
);
9495

96+
const toVersionShort = shortVersionId(toVersion);
9597
if (migratedSelection) {
96-
updated = `${ORIGIN}#/${meta.latestShort}/${encodePageNumberAndIndex(migratedSelection.selectionStart)}${encodePageNumberAndIndex(migratedSelection.selectionEnd)}`;
98+
updated = `${ORIGIN}#/${toVersionShort}/${encodePageNumberAndIndex(migratedSelection.selectionStart)}${encodePageNumberAndIndex(migratedSelection.selectionEnd)}`;
9799
migrated = true;
98100
} else {
99-
const latestSynctex = await synctexStore.getSynctex(meta.metadata.latest);
100-
const hasStart = !!latestSynctex.blocksByPage.get(selectionStart.pageNumber)?.[selectionStart.index];
101-
const hasEnd = !!latestSynctex.blocksByPage.get(selectionEnd.pageNumber)?.[selectionEnd.index];
101+
const toVersionSynctex = await synctexStore.getSynctex(toVersion);
102+
const hasStart = !!toVersionSynctex.blocksByPage.get(selectionStart.pageNumber)?.[selectionStart.index];
103+
const hasEnd = !!toVersionSynctex.blocksByPage.get(selectionEnd.pageNumber)?.[selectionEnd.index];
102104

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

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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ async function main() {
2626
"--ignore-file <path>",
2727
"Path to a file containing patterns to ignore. Gitignore format applies. Patterns are resolved according to current working directory.",
2828
)
29-
.option("--write", "Modify the files and update reader links to their newest versions.")
29+
.option("--version <name>", "Commit hash of specific version to update to (instead of latest)")
30+
.option("--write", "Modify the files and update reader links to requested/lastest versions.")
3031
.option("--fix", "Alias for --write.")
3132
.option("--generate-notes <file.json>", "Generate notes for the Gray Paper Reader")
3233
.action(async (paths, options) => {
@@ -82,7 +83,7 @@ async function main() {
8283
console.time(label);
8384
let report: Report | null = null;
8485
try {
85-
report = await scan(files, metadata);
86+
report = await scan(files, metadata, options.version);
8687
} finally {
8788
console.timeEnd(label);
8889
}

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 = "latest") {
70+
const v = version === "latest" ? metadata.metadata.latest : version;
71+
const versionData = metadata.metadata.versions[v];
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)