Skip to content

Commit 5c7f420

Browse files
committed
fix: sillsdev#4 (ordering) and sillsdev#19, (dashes in categories)
1 parent 35abd64 commit 5c7f420

10 files changed

+125
-60
lines changed

jest.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ module.exports = {
44
transform: {
55
"^.+\\.ts?$": "ts-jest",
66
},
7-
transformIgnorePatterns: ["<rootDir>/node_modules/"],
7+
modulePathIgnorePatterns: ["<rootDir>/node_modules/", "<rootDir>/dist/"],
8+
transformIgnorePatterns: ["<rootDir>/node_modules/", "<rootDir>/dist/"],
89
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"dist/**/*"
88
],
99
"scripts": {
10-
"build": "tsc && cp ./src/css/*.css dist/",
10+
"test": "jest",
11+
"build": "yarn test && tsc && cp ./src/css/*.css dist/",
1112
"clean": "rm -rf ./dist/",
1213
"semantic-release": "semantic-release",
1314
"typecheck": "tsc --noEmit",

src/FlatGuidLayoutStrategy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export class FlatGuidLayoutStrategy extends LayoutStrategy {
1414
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1515
public newLevel(
1616
rootDir: string,
17+
order: number,
1718
context: string,
1819
_levelLabel: string
1920
): string {

src/HierarchicalNamedLayoutStrategy.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,24 @@ import { NotionPage } from "./NotionPage";
1010
export class HierarchicalNamedLayoutStrategy extends LayoutStrategy {
1111
public newLevel(
1212
dirRoot: string,
13+
order: number,
1314
context: string,
1415
levelLabel: string
1516
): string {
16-
const path = context + "/" + sanitize(levelLabel).replaceAll(" ", "-");
17+
// The docusaurus documentation doesn't specify how the prefix should look (such that it recognizes and strips it)
18+
// A following dash works.
19+
const prefix = "";
20+
const path = context + "/" + prefix + sanitize(levelLabel); //.replaceAll(" ", "-");
1721

1822
//console.log("Creating level " + path);
19-
fs.mkdirSync(dirRoot + "/" + path, { recursive: true });
23+
const newPath = dirRoot + "/" + path;
24+
fs.mkdirSync(newPath, { recursive: true });
25+
this.addCategoryMetadata(newPath, order);
2026
return path;
2127
}
2228

2329
public getPathForPage(page: NotionPage, extensionWithDot: string): string {
24-
let path =
25-
this.rootDirectory +
26-
"/" +
27-
page.context +
28-
"/" +
29-
sanitize(page.nameForFile()) +
30-
extensionWithDot;
31-
32-
path = path
30+
const sanitizedName = sanitize(page.nameForFile())
3331
.replaceAll("//", "/")
3432
.replaceAll("%20", "-")
3533
.replaceAll(" ", "-")
@@ -40,9 +38,30 @@ export class HierarchicalNamedLayoutStrategy extends LayoutStrategy {
4038
.replaceAll("”", "")
4139
.replaceAll("'", "")
4240
.replaceAll("?", "-");
43-
// console.log(
44-
// `getPathForPage(${context}, ${pageId}, ${title}) with root ${this.rootDirectory} --> ${path}`
45-
// );
41+
42+
const context = ("/" + page.context + "/").replaceAll("//", "/");
43+
const path =
44+
this.rootDirectory + context + sanitizedName + extensionWithDot;
45+
4646
return path;
4747
}
48+
49+
//{
50+
// "position": 2.5,
51+
// "label": "Tutorial",
52+
// "collapsible": true,
53+
// "collapsed": false,
54+
// "className": "red",
55+
// "link": {
56+
// "type": "generated-index",
57+
// "title": "Tutorial overview"
58+
// },
59+
// "customProps": {
60+
// "description": "This description can be used in the swizzled DocCard"
61+
// }
62+
// }
63+
private addCategoryMetadata(dir: string, order: number) {
64+
const data = `{"position":${order}}`;
65+
fs.writeFileSync(dir + "/_category_.json", data);
66+
}
4867
}

src/LayoutStrategy.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export abstract class LayoutStrategy {
2424

2525
public abstract newLevel(
2626
rootDir: string,
27-
ontext: string,
27+
order: number,
28+
context: string,
2829
levelLabel: string
2930
): string;
3031
public abstract getPathForPage(
@@ -34,7 +35,7 @@ export abstract class LayoutStrategy {
3435

3536
public getLinkPathForPage(page: NotionPage): string {
3637
// the url we return starts with a "/", meaning it is relative to the root of the markdown root (e.g. /docs root in Docusaurus)
37-
return this.getPathForPage(page, "").replace(this.rootDirectory, "");
38+
return ("/" + page.slug).replaceAll("//", "/");
3839
}
3940

4041
public pageWasSeen(page: NotionPage): void {

src/NotionImage-CaptionReading.spec.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
1-
import { parseImageBlock } from "./images";
1+
import { initImageHandling, parseImageBlock } from "./images";
22

33
const kPrimaryImageUrl =
44
"https://s3.us-west-2.amazonaws.com/primaryImage.png?Blah=foo";
55

6+
/* didn't work?
7+
beforeAll(async () => {
8+
console.log("before");
9+
await initImageHandling("", "", []);
10+
console.log("azfter");
11+
});
12+
*/
613
/* eslint-disable @typescript-eslint/require-await */
714
test("finds primary image url", async () => {
8-
const img = parseImageBlock(kImageBlockWithTwoLocalizedImages);
15+
await initImageHandling("", "", []);
16+
const img = parseImageBlock(kImageBlockWithTwoLocalizedImages.image);
917
expect(img.primaryUrl).toBe(kPrimaryImageUrl);
1018
});
1119

1220
test("primary caption content after image links are removed", async () => {
21+
await initImageHandling("", "", []);
1322
const img = parseImageBlock(
14-
kImageBlockWithTwoLocalizedImagesWrappedWithActualCaptionText
23+
kImageBlockWithTwoLocalizedImagesWrappedWithActualCaptionText.image
1524
);
1625
// carriage returns seem to mess up the markdown, so should be removed
1726
expect(img.caption).toBe("Caption before images. Caption after images.");
1827
});
1928

2029
test("gets localized image links", async () => {
30+
await initImageHandling("", "", []);
2131
const img = parseImageBlock(
22-
kImageBlockWithTwoLocalizedImagesWrappedWithActualCaptionText
32+
kImageBlockWithTwoLocalizedImagesWrappedWithActualCaptionText.image
2333
);
2434
expect(img.localizedUrls.length).toBe(2);
2535
expect(img.localizedUrls[0].iso632Code).toBe("fr");

src/NotionPage.ts

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import { RateLimiter } from "limiter";
88
import { Client } from "@notionhq/client";
99
import { logDebug } from "./log";
10+
import { info } from "console";
1011

1112
const notionLimiter = new RateLimiter({
1213
tokensPerInterval: 3,
@@ -33,17 +34,20 @@ export function initNotionClient(notionToken: string): Client {
3334
export class NotionPage {
3435
private metadata: GetPageResponse;
3536
public readonly pageId: string;
37+
public readonly order: number;
3638
public context: string; // where we found it in the hierarchy of the outline
3739
public foundDirectlyInOutline: boolean; // the page was found as a descendent of /outline instead of being linked to
3840

3941
private constructor(
4042
context: string,
4143
pageId: string,
44+
order: number,
4245
metadata: GetPageResponse,
4346
foundDirectlyInOutline: boolean
4447
) {
4548
this.context = context;
4649
this.pageId = pageId;
50+
this.order = order;
4751
this.metadata = metadata;
4852
this.foundDirectlyInOutline = foundDirectlyInOutline;
4953

@@ -55,11 +59,19 @@ export class NotionPage {
5559
public static async fromPageId(
5660
context: string,
5761
pageId: string,
62+
order: number,
5863
foundDirectlyInOutline: boolean
5964
): Promise<NotionPage> {
6065
const metadata = await getPageMetadata(pageId);
61-
//logDebug(JSON.stringify(metadata));
62-
return new NotionPage(context, pageId, metadata, foundDirectlyInOutline);
66+
67+
//logDebug("notion metadata", JSON.stringify(metadata));
68+
return new NotionPage(
69+
context,
70+
pageId,
71+
order,
72+
metadata,
73+
foundDirectlyInOutline
74+
);
6375
}
6476

6577
public matchesLinkId(id: string): boolean {
@@ -162,7 +174,6 @@ export class NotionPage {
162174
block_id: this.pageId,
163175
page_size: 100, // max hundred links in a page
164176
});
165-
166177
return children;
167178
}
168179

@@ -257,19 +268,21 @@ export class NotionPage {
257268
}
258269

259270
public async getContentInfo(): Promise<{
260-
childPages: any[];
261-
linksPages: any[];
271+
childPageIdsAndOrder: { id: string; order: number }[];
272+
linksPageIdsAndOrder: { id: string; order: number }[];
262273
hasParagraphs: boolean;
263274
}> {
264275
const children = await this.getChildren();
265-
276+
for (let i = 0; i < children.results.length; i++) {
277+
(children.results[i] as any).order = i;
278+
}
266279
return {
267-
childPages: children.results
280+
childPageIdsAndOrder: children.results
268281
.filter((b: any) => b.type === "child_page")
269-
.map((b: any) => b.id),
270-
linksPages: children.results
282+
.map((b: any) => ({ id: b.id, order: b.order })),
283+
linksPageIdsAndOrder: children.results
271284
.filter((b: any) => b.type === "link_to_page")
272-
.map((b: any) => b.link_to_page.page_id),
285+
.map((b: any) => ({ id: b.link_to_page.page_id, order: b.order })),
273286
hasParagraphs: children.results.some(
274287
b =>
275288
(b as any).type === "paragraph" &&

src/images.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ async function saveImage(imageSet: ImageSet): Promise<void> {
155155
localizedImage.iso632Code
156156
}/docusaurus-plugin-content-docs/current/${imageSet.relativePathToParentDocument!}`;
157157

158-
writeImageIfNew(directory + "/" + imageSet.outputFileName!, buffer);
158+
writeImageIfNew(
159+
(directory + "/" + imageSet.outputFileName!).replaceAll("//", "/"),
160+
buffer
161+
);
159162
}
160163
}
161164

@@ -177,6 +180,7 @@ function writeImageIfNew(path: string, buffer: Buffer) {
177180
}
178181

179182
export function parseImageBlock(image: any): ImageSet {
183+
if (!locales) throw Error("Did you call initImageHandling()?");
180184
const imageSet: ImageSet = {
181185
primaryUrl: "",
182186
caption: "",

src/links.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,17 @@ function transformLinks(
8383

8484
// The key to understanding this while is that linkRegExp actually has state, and
8585
// it gives you a new one each time. https://stackoverflow.com/a/1520853/723299
86-
verbose(`transformLinks ${pageMarkdown}`);
86+
8787
while ((match = linkRegExp.exec(pageMarkdown)) !== null) {
88-
const string = match[0];
88+
const originalLink = match[0];
8989

9090
const hrefFromNotion = match[2];
9191
const text = convertLinkText(match[1] || "", hrefFromNotion);
9292
const hrefForDocusaurus = convertHref(hrefFromNotion);
9393

9494
if (hrefForDocusaurus) {
95-
output = output.replace(string, `[${text}](${hrefForDocusaurus})`);
95+
output = output.replace(originalLink, `[${text}](${hrefForDocusaurus})`);
96+
verbose(`transformed link: ${originalLink}-->${hrefForDocusaurus}`);
9697
} else {
9798
verbose(`Maybe problem with link ${JSON.stringify(match)}`);
9899
}

0 commit comments

Comments
 (0)