Skip to content

Commit

Permalink
feat(publishing): generate toc
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinslin committed Oct 20, 2020
1 parent 713c04e commit 53ee270
Show file tree
Hide file tree
Showing 6 changed files with 276 additions and 13 deletions.
Expand Up @@ -123,6 +123,58 @@ exports[`note refs note refs, recursive 1`] = `
"
`;
exports[`toc generate toc 1`] = `
"---
id: bar
title: Bar
desc: ''
updated: '1'
created: '1'
parent: null
children: []
toc: true
nav_order: 0
fname: bar
hpath: bar
permalink: /
---
# Header1
## Table of Contents
- [Header 1.1](#header-11)
- [Header 2](#header-2)
## Header 1.1
## Header 2
"
`;
exports[`toc no generate toc 1`] = `
"---
id: bar
title: Bar
desc: ''
updated: '1'
created: '1'
parent: null
children: []
nav_order: 0
fname: bar
hpath: bar
permalink: /
---
# Header1
## Table of Contents
## Header 1.1
## Header 2
"
`;
exports[`wiki link case sensitive link 1`] = `
"[foo.Mixed_case](id.foo.mixed-case)
"
Expand Down
126 changes: 125 additions & 1 deletion packages/dendron-cli/src/commands/__tests__/build-site.v2.spec.ts
Expand Up @@ -18,7 +18,11 @@ import _ from "lodash";
import path from "path";
import process from "process";
import { BuildSiteCommand } from "../build-site";
import { NodeTestUtilsV2 } from "@dendronhq/common-test-utils";
import {
EngineTestUtilsV2,
NodeTestPresetsV2,
NodeTestUtilsV2,
} from "@dendronhq/common-test-utils";

describe("buildSite", () => {
let vaultPath: string;
Expand Down Expand Up @@ -661,3 +665,123 @@ describe("note refs", () => {
expect(content.indexOf("portal-container") >= 0).toBeFalsy();
});
});

describe("toc", () => {
let vaultDir: string;
let engine: DEngine;
let siteRootDir: string;
let notesDir: string;
let writeStubs = false;
let engineClient: DEngineClientV2;
let port: number;

beforeAll(async () => {
const logPath = process.env["LOG_PATH"];
port = await launch({ logPath });
});

beforeEach(async () => {
siteRootDir = FileTestUtils.tmpDir().name;
notesDir = path.join(siteRootDir, "notes");
vaultDir = await EngineTestUtilsV2.setupVault({
initDirCb: async (vaultDir) => {
await NodeTestPresetsV2.createOneNoteOneSchemaPresetWithBody({
vaultDir,
});
},
});
});

afterEach(() => {
fs.removeSync(vaultDir);
});

test("generate toc", async () => {
await NodeTestUtilsV2.createNote({
vaultDir,
noteProps: {
id: "bar",
custom: {
toc: true,
},
fname: "bar",
body: [
"# Header1",
"## Table of Contents",
"## Header 1.1",
"",
"## Header 2",
].join("\n"),
},
});
const wsRoot = path.join(vaultDir, "../");
engineClient = DendronEngineClient.create({
port,
vaults: [vaultDir],
ws: wsRoot,
});
await engineClient.init();

const config: DendronSiteConfig = {
siteHierarchies: ["bar"],
siteRootDir,
usePrettyRefs: true,
};
const cmd = new BuildSiteCommand();
await cmd.execute({
engine,
engineClient,
config,
wsRoot,
writeStubs,
});
let fooPath = path.join(notesDir, "bar.md");
const content = fs.readFileSync(fooPath, { encoding: "utf8" });
// const { content } = readMD(fooPath);
expect(content).toMatchSnapshot();
expect(content.indexOf("[Header 1.1](#header-11)") >= 0).toBeTruthy();
});

test("no generate toc", async () => {
await NodeTestUtilsV2.createNote({
vaultDir,
noteProps: {
id: "bar",
fname: "bar",
body: [
"# Header1",
"## Table of Contents",
"## Header 1.1",
"",
"## Header 2",
].join("\n"),
},
});
const wsRoot = path.join(vaultDir, "../");
engineClient = DendronEngineClient.create({
port,
vaults: [vaultDir],
ws: wsRoot,
});
await engineClient.init();

const config: DendronSiteConfig = {
siteHierarchies: ["bar"],
siteRootDir,
usePrettyRefs: true,
};
const cmd = new BuildSiteCommand();
await cmd.execute({
engine,
engineClient,
config,
wsRoot,
writeStubs,
});
let fooPath = path.join(notesDir, "bar.md");
const content = fs.readFileSync(fooPath, { encoding: "utf8" });
// const { content } = readMD(fooPath);
expect(content).toMatchSnapshot();
expect(content.indexOf("[Header 1.1](#header-11)") >= 0).toBeFalsy();
});
});
30 changes: 18 additions & 12 deletions packages/dendron-cli/src/commands/build-site.ts
Expand Up @@ -19,6 +19,7 @@ import {
ParserUtilsV2,
replaceRefs,
stripLocalOnlyTags,
toc,
} from "@dendronhq/engine-server";
import fs from "fs-extra";
import matter from "gray-matter";
Expand Down Expand Up @@ -208,21 +209,26 @@ async function note2JekyllMdFile(
},
});
}

proc = proc.use(replaceRefs, {
wikiLink2Md: true,
wikiLinkPrefix: linkPrefix,
imageRefPrefix: opts.assetsPrefix,
wikiLinkUseId: true,
engine: opts.engine,
scratch: scratchPad2,
forNoteRefInSite: true,
});

if (note?.custom?.toc) {
proc = proc.use(toc);
}

// replaces wiki-links with the markdown equivalent
note.body = proc
.use(replaceRefs, {
wikiLink2Md: true,
wikiLinkPrefix: linkPrefix,
imageRefPrefix: opts.assetsPrefix,
wikiLinkUseId: true,
engine: opts.engine,
scratch: scratchPad2,
forNoteRefInSite: true,
})
.processSync(note.body)
.toString();
note.body = proc.processSync(note.body).toString();
} catch (err) {
console.log(err);
throw err;
}
const filePath = path.join(opts.notesDir, meta.id + ".md");
await fs.writeFile(
Expand Down
78 changes: 78 additions & 0 deletions packages/engine-server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/engine-server/package.json
Expand Up @@ -67,6 +67,7 @@
"remark": "^12.0.1",
"remark-frontmatter": "^2.0.0",
"remark-parse": "^8.0.3",
"remark-toc": "^7.0.0",
"remark-wiki-link": "^0.0.4",
"stream": "^0.0.2",
"through2": "^4.0.2",
Expand Down
2 changes: 2 additions & 0 deletions packages/engine-server/src/topics/markdown/index.ts
@@ -1,3 +1,5 @@
export * from "./utils";
export * from "./utilsv2";
export * from "./plugins";
const toc = require("remark-toc");
export { toc };

0 comments on commit 53ee270

Please sign in to comment.