Skip to content

Commit

Permalink
Handle default namespace change in 1.18
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Aug 14, 2023
1 parent fe6aa71 commit e8c4803
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 455 deletions.
25 changes: 21 additions & 4 deletions lib/convertGuide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,28 @@ import convertCategoryIndex from "./convertCategoryIndex.js";
import convertItemIndex from "./convertItemIndex.js";
import convertRecipes from "./convertRecipes.js";
import convertColoredVersions from "./convertColoredVersions.js";
import semver from "semver";

function writeGuide(srcDir: string, destDir: string, gameData: any) {
let defaultNamespace;
const gameVersion = semver.coerce(gameData.gameVersion);
if (!gameVersion) {
throw new Error("Failed to coerce game version: " + gameData.gameVersion);
}
if (semver.lt(gameVersion, "1.18.0")) {
defaultNamespace = "appliedenergistics2";
} else {
defaultNamespace = "ae2";
}
console.info(
"Game version: %s. Default Namespace: %s",
gameVersion,
defaultNamespace,
);

const index: GuideIndex = {
defaultNamespace: "ae2",
startPageId: "ae2:getting-started.md",
defaultNamespace,
startPageId: `${defaultNamespace}:getting-started.md`,
navigationRootNodes: [],
coloredVersions: {},
fluids: {},
Expand All @@ -27,13 +44,13 @@ function writeGuide(srcDir: string, destDir: string, gameData: any) {
items: {},
};

const pages = loadPages(srcDir);
const pages = loadPages(index, srcDir);
console.info("Loaded %d pages", Object.keys(pages).length);

const pageTree = buildPageTree(Object.values(pages));

convertItems(gameData, index);
convertNavigation(srcDir, gameData, index, pages);
convertNavigation(srcDir, index, pages);
convertPages(pageTree, pages, index);
convertCategoryIndex(pages, index);
convertItemIndex(pages, index);
Expand Down
2 changes: 1 addition & 1 deletion lib/convertItemIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function convertItemIndex(

for (const page of Object.values(pages)) {
for (let itemId of page.itemIds) {
itemId = qualifyId(itemId);
itemId = qualifyId(index, itemId);

const existingPageId = itemIdToPage.get(itemId);
if (existingPageId) {
Expand Down
6 changes: 3 additions & 3 deletions lib/convertNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ interface MenuEntry {
}

function convertMenuEntryToNavigationNode(
index: GuideIndex,
entry: string | PageLink,
pages: Record<string, LoadedPage>,
): NavigationNode {
const pagePath = typeof entry === "string" ? entry : entry.path;
const pageId = pagePathToId(pagePath);
const pageId = pagePathToId(index, pagePath);
const page = pages[pageId];
if (!page) {
throw new Error("Menu contains unknown page: " + pagePath);
Expand All @@ -38,7 +39,6 @@ function convertMenuEntryToNavigationNode(

export default function convertNavigation(
srcDir: string,
gameData: any,
guideIndex: GuideIndex,
pages: Record<string, LoadedPage>,
) {
Expand All @@ -55,7 +55,7 @@ export default function convertNavigation(
icon: undefined,
position: 0,
children: menuEntry.children.map((entry) =>
convertMenuEntryToNavigationNode(entry, pages),
convertMenuEntryToNavigationNode(guideIndex, entry, pages),
),
});
}
Expand Down
20 changes: 14 additions & 6 deletions lib/loadPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import path from "path";
import { frontmatter } from "micromark-extension-frontmatter";
import YAML from "yaml";
import { pagePathToId, qualifyId } from "./utils.js";
import { GuideIndex } from "./targetTypes.js";

export type LoadedPage = {
/**
Expand Down Expand Up @@ -45,7 +46,11 @@ function extractFrontmatter(root: Root) {
return frontmatterDict;
}

function loadPage(pagePath: string, pageContent: string): LoadedPage {
function loadPage(
guideIndex: GuideIndex,
pagePath: string,
pageContent: string,
): LoadedPage {
const root = fromMarkdown(pageContent, {
extensions: [frontmatter(["yaml"]), gfm(), mdx()],
mdastExtensions: [
Expand All @@ -69,13 +74,13 @@ function loadPage(pagePath: string, pageContent: string): LoadedPage {
delete frontmatterDict["title"];
delete frontmatterDict["categories"];
itemIds ??= [];
itemIds = itemIds.map(qualifyId);
itemIds = itemIds.map((itemId: string) => qualifyId(guideIndex, itemId));
categories ??= [];
sidenavIcon ??= itemIds[0];
sidenavIcon = qualifyId(sidenavIcon);
sidenavIcon = qualifyId(guideIndex, sidenavIcon);

return {
id: pagePathToId(pagePath),
id: pagePathToId(guideIndex, pagePath),
path: pagePath,
frontmatter: frontmatterDict,
root,
Expand All @@ -87,12 +92,15 @@ function loadPage(pagePath: string, pageContent: string): LoadedPage {
};
}

export default function loadPages(srcDir: string): Record<string, LoadedPage> {
export default function loadPages(
guideIndex: GuideIndex,
srcDir: string,
): Record<string, LoadedPage> {
const pages: Record<string, LoadedPage> = {};
const contentRoot = path.join(srcDir, "content");
for (const pagePath of globSync("**/*.md", { cwd: contentRoot })) {
const pageContent = readFileSync(path.join(contentRoot, pagePath), "utf-8");
const page = loadPage(pagePath, pageContent);
const page = loadPage(guideIndex, pagePath, pageContent);
pages[page.id] = page;
}
return pages;
Expand Down
16 changes: 10 additions & 6 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { createHash } from "crypto";
import baseX from "base-x";
import path from "path";
import { writeFileSync } from "fs";
import { GuideIndex } from "./targetTypes.js";

export function pagePathToId(path: string) {
return qualifyId(path.replaceAll("\\", "/"));
export function pagePathToId(index: GuideIndex, path: string) {
return qualifyId(index, path.replaceAll("\\", "/"));
}

const BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Expand All @@ -30,11 +31,14 @@ export function writeCacheBusted(targetPath: string, buffer: Buffer) {
}

// Old guides used ae2: as the default namespace
export function qualifyId(id: string): string;
export function qualifyId(id: undefined): undefined;
export function qualifyId(id: string | undefined): string | undefined {
export function qualifyId(index: GuideIndex, id: string): string;
export function qualifyId(index: GuideIndex, id: undefined): undefined;
export function qualifyId(
index: GuideIndex,
id: string | undefined,
): string | undefined {
if (id && !id.includes(":")) {
return "ae2:" + id;
return index.defaultNamespace + ":" + id;
} else {
return id;
}
Expand Down
Loading

0 comments on commit e8c4803

Please sign in to comment.