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

MarkdownPlugin populates route property #20

Merged
merged 7 commits into from
Mar 21, 2017
Merged
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
MarkdownPlugin buildNavTree is trivial now, thanks to PageMap#toTree
  • Loading branch information
Gilad Gray committed Mar 20, 2017
commit 2a4f8684d2c9b34b427a67a0fc0826fcc5100b3b
77 changes: 2 additions & 75 deletions src/plugins/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,7 @@
* repository.
*/

import {
headingReference,
IHeadingNode,
IPageData,
IPageNode,
isPageNode,
isTag,
pageReference,
slugify,
StringOrTag,
} from "../client";
import { headingReference, IPageData, IPageNode, pageReference, StringOrTag } from "../client";
import { PageMap } from "../page";
import { ICompiler, IFile, IPlugin } from "./plugin";

Expand Down Expand Up @@ -108,69 +98,6 @@ export class MarkdownPlugin implements IPlugin<IMarkdownPluginData> {
}

private buildNavTree(pages: PageMap) {
// navPage is used to construct the sidebar menu
const navRoot = pages.get(this.options.navPage);
if (navRoot === undefined) {
console.warn(`navPage '${this.options.navPage}' not found, returning empty array.`);
return [];
}

const roots = createNavigableTree(pages, navRoot).children as IPageNode[];
// nav page is not a real docs page so we can remove it from output
pages.remove(this.options.navPage);
roots.forEach((page) => {
if (isPageNode(page)) {
page.children.forEach((child) => this.nestChildPage(pages, page, child));
}
});

return roots;
}

private nestChildPage(pages: PageMap, parent: IPageNode, child: IPageNode | IHeadingNode) {
const originalRef = child.reference;

// update entry reference to include parent reference
const nestedRef = isPageNode(child)
? this.options.pageReference(parent.reference, child.reference)
: this.options.headingReference(parent.reference, child.title);
child.reference = nestedRef;

if (isPageNode(child)) {
// rename nested pages to be <parent>.<child> and remove old <child> entry.
// (we know this ref exists because isPageNode(child) and originalRef = child.reference)
const page = pages.remove(originalRef)!;
pages.set(nestedRef, { ...page, reference: nestedRef });
// recurse through page children
child.children.forEach((innerchild) => this.nestChildPage(pages, child, innerchild));
}
return pages.toTree(this.options.navPage).children as IPageNode[];
}
}

function createNavigableTree(pages: PageMap, page: IPageData, depth = 0) {
const pageNode: IPageNode = initPageNode(page, depth);
page.contents.forEach((node: StringOrTag, i: number) => {
if (isTag(node)) {
if (node.tag === "page") {
const subpage = pages.get(node.value);
if (subpage === undefined) {
throw new Error(`Unknown @page '${node.value}' referenced in '${page.reference}'`);
}
pageNode.children.push(createNavigableTree(pages, subpage, depth + 1));
}
if (i !== 0 && node.tag.match(/^#+$/)) {
// use heading strength - 1 cuz h1 is the title
pageNode.children.push(initHeadingNode(node.value, depth + node.tag.length - 1));
}
}
});
return pageNode;
}

function initPageNode({ reference, title }: IPageData, depth: number): IPageNode {
return { children: [], depth, reference, title };
}

function initHeadingNode(title: string, depth: number): IHeadingNode {
return { depth, reference: slugify(title), title };
}