Skip to content
Merged
Prev Previous commit
Next Next commit
Fix the epub generator
  • Loading branch information
orta committed Jul 24, 2020
commit a16ed1da33b1ab18c0a387e3876ab5721c375404
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ const handbookPages = [
{ file: "Variable Declarations.md" },
],
},

{
title: "Tutorials",
summary: "Using TypeScript in several environments.",
Expand Down Expand Up @@ -230,13 +229,13 @@ for (const lang of langs) {
`);
addItems(subItem);
codeForTheHandbook.push(",");
} else {
} else if ("file" in subItem) {
// It's a file reference
const subNavInfo =
langInfo[lang].get(subItem.file) ||
langInfo["en"].get(subItem.file);

if (!subNavInfo) throwForUnfoundFile(subItem, lang);
if (!subNavInfo) throwForUnfoundFile(subItem, lang, langInfo["en"]);

codeForTheHandbook.push(`
title: "${subNavInfo.data.short || subNavInfo.data.title}",
Expand Down Expand Up @@ -271,8 +270,6 @@ codeForTheHandbook.push(`
return navigations[lang]
}`);

// console.log(codeForTheHandbook.join("\n"));

// prettier-ignore
const pathToFileWeEdit = join(__dirname, "..", "..", "typescriptlang-org", "src", "lib", "documentationNavigation.ts");
const startMarker = "/** ---INSERT--- */";
Expand All @@ -292,8 +289,6 @@ writeFileSync(
format(newCode, { filepath: pathToFileWeEdit })
);

/// ------------------

/**
* @typedef {Object} HandbookNavSubItem
* @property {import("./types/AllFilenames").AllDocsPages= } file - the reference to the file based on the lang root
Expand Down Expand Up @@ -335,10 +330,10 @@ function validateMarkdownFile(info, filepath) {
}
}

function throwForUnfoundFile(subItem, lang) {
throw new Error(
`Could not find the file '${subItem.file}' from the handbook nav in either ${lang} or 'en'`
);
function throwForUnfoundFile(subItem, lang, langInfo) {
const keys = [...langInfo.keys()];
// prettier-ignore
throw new Error(`Could not find the file '${subItem.file}' from the handbook nav in either ${lang} or 'en' - has: ${keys.join(", ")}`);
}

function fillReleaseInfo() {
Expand Down
7 changes: 3 additions & 4 deletions packages/handbook-epub/script/createEpub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ readdirSync(handbookPath, "utf-8").forEach((path) => {
const md = readMarkdownFile(filePath);
// prettier-ignore
if (!md.data.permalink) throw new Error(`${path} in the handbook did not have a permalink in the yml header`);

const id = md.data.permalink;
markdowns.set(id, md);
});
Expand Down Expand Up @@ -101,9 +100,9 @@ const startEpub = async () => {
});
epub.write(Streampub.newChapter(bookMetadata.title, editedIntro, 0));

for (const item of handbook.items) {
const index = handbook.items.indexOf(item) + 1;
await addHandbookPage(epub, item.id, index);
for (const item of handbook!.items!) {
const index = handbook!.items!.indexOf(item) + 1;
await addHandbookPage(epub, item.permalink!, index);
}

epub.end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
getDocumentationNavForLanguage,
getNextPageID,
getPreviousPageID,
NewNavItem,
SidebarNavItem,
} from "../../../src/lib/documentationNavigation"

export const createDocumentationPages = async (
Expand Down Expand Up @@ -50,7 +50,7 @@ export const createDocumentationPages = async (
const anyData = result.data as any
const docs = anyData.allFile.nodes as any

const findWithPage = (item: NewNavItem, permalink: string) => {
const findWithPage = (item: SidebarNavItem, permalink: string) => {
if (item.permalink === permalink) return item.id
if (!item.items) return false
for (const subItem of item.items) {
Expand Down
8 changes: 4 additions & 4 deletions packages/typescriptlang-org/src/components/layout/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { Link } from "gatsby"

import "./Sidebar.scss"
import { onAnchorKeyDown, onButtonKeydown } from "./Sidebar-keyboard"
import { NewNavItem } from "../../lib/documentationNavigation"
import { SidebarNavItem } from "../../lib/documentationNavigation"

export type Props = {
navItems: NewNavItem[]
navItems: SidebarNavItem[]
selectedID: string
openAllSectionsExceptWhatsNew?: true
}
Expand Down Expand Up @@ -65,7 +65,7 @@ export const Sidebar = (props: Props) => {
})
}, [])

const RenderItem = (props: { item: NewNavItem, selectedID: string, openAllSectionsExceptWhatsNew?: boolean }) => {
const RenderItem = (props: { item: SidebarNavItem, selectedID: string, openAllSectionsExceptWhatsNew?: boolean }) => {
const item = props.item
if (!item.items) {
// Is it the leaf in the nav?
Expand All @@ -82,7 +82,7 @@ export const Sidebar = (props: Props) => {
</li>
} else {
// Has children
const findSelected = (item: NewNavItem) => {
const findSelected = (item: SidebarNavItem) => {
if (item.id === props.selectedID) return true
if (!item.items) return false
for (const subItem of item.items) {
Expand Down
78 changes: 39 additions & 39 deletions packages/typescriptlang-org/src/lib/documentationNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@
Find the source of truth at packages/documentation/scripts/generateDocsNavigationPerLanguage.js
*/

export interface NewNavItem {
title: string;
id: string;
permalink?: string;
chronological?: boolean;
oneline?: string;
items?: NewNavItem[];
export interface SidebarNavItem {
title: string
id: string
permalink?: string
chronological?: boolean
oneline?: string
items?: SidebarNavItem[]
}

/** ---INSERT--- */

export function getDocumentationNavForLanguage(
langRequest: string
): NewNavItem[] {
const langs = ["en", "vo"];
const lang = langs.includes(langRequest) ? langRequest : "en";
const navigations: Record<string, NewNavItem[]> = {};
): SidebarNavItem[] {
const langs = ["en", "vo"]
const lang = langs.includes(langRequest) ? langRequest : "en"
const navigations: Record<string, SidebarNavItem[]> = {}

navigations.en = [
{
Expand Down Expand Up @@ -655,7 +655,7 @@ export function getDocumentationNavForLanguage(
},
],
},
];
]
navigations.vo = [
{
title: "Get Started",
Expand Down Expand Up @@ -1287,65 +1287,65 @@ export function getDocumentationNavForLanguage(
},
],
},
];
]

return navigations[lang];
return navigations[lang]
}

/** ---INSERT-END--- */

const findInNav = (
item: NewNavItem | NewNavItem[],
fun: (item: NewNavItem) => boolean
): NewNavItem | undefined => {
item: SidebarNavItem | SidebarNavItem[],
fun: (item: SidebarNavItem) => boolean
): SidebarNavItem | undefined => {
if (Array.isArray(item)) {
for (const subItem of item) {
const sub = findInNav(subItem, fun);
if (sub) return sub;
const sub = findInNav(subItem, fun)
if (sub) return sub
}
} else {
if (fun(item)) return item;
if (!item.items) return undefined;
if (fun(item)) return item
if (!item.items) return undefined
for (const subItem of item.items) {
const sub = findInNav(subItem, fun);
if (sub) return sub;
const sub = findInNav(subItem, fun)
if (sub) return sub
}
return undefined;
return undefined
}
};
}

export function getNextPageID(navs: NewNavItem[], currentID: string) {
export function getNextPageID(navs: SidebarNavItem[], currentID: string) {
// prettier-ignore
const section = findInNav(navs, (i) => i && !!i.items && !!i.items.find(i => i.id === currentID)) || false
if (!section) return undefined;
if (!section.chronological) return undefined;
if (!section.items) return;
if (!section) return undefined
if (!section.chronological) return undefined
if (!section.items) return

const currentIndex = section.items.findIndex((i) => i.id === currentID);
const next = section.items[currentIndex + 1];
const currentIndex = section.items.findIndex(i => i.id === currentID)
const next = section.items[currentIndex + 1]
if (next) {
return {
path: next.permalink,
...section.items[currentIndex + 1],
};
}
}
}

export function getPreviousPageID(navs: NewNavItem[], currentID: string) {
export function getPreviousPageID(navs: SidebarNavItem[], currentID: string) {
// prettier-ignore
const section = findInNav(navs, (i) => i && !!i.items && !!i.items.find(i => i.id === currentID)) || false

if (!section) return undefined;
if (!section.chronological) return undefined;
if (!section.items) return;
if (!section) return undefined
if (!section.chronological) return undefined
if (!section.items) return

const currentIndex = section.items.findIndex((i) => i.id === currentID);
const prev = section.items[currentIndex - 1];
const currentIndex = section.items.findIndex(i => i.id === currentID)
const prev = section.items[currentIndex - 1]

if (prev) {
return {
path: prev.permalink,
...section.items[currentIndex - 1],
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Intl } from "../components/Intl"
// This dependency is used in gatsby-remark-autolink-headers to generate the slugs
import slugger from "github-slugger"

import "./handbook.scss"
import "./documentation.scss"
import "./markdown.scss"

import { NextPrev } from "../components/handbook/NextPrev"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Layout } from "../../../../components/layout"
import { Link } from "gatsby"

import "../../css/documentation.scss"
import "../../../handbook.scss"
import "../../../documentation.scss"

import { Intl } from "../../../../components/Intl"

Expand Down