Skip to content

Commit

Permalink
Merge pull request #40 from kmlbgn/master
Browse files Browse the repository at this point in the history
fix: links pointing to relative slugs
  • Loading branch information
kmlbgn authored May 16, 2024
2 parents fdf7bef + a2e63f4 commit 7163f8b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
17 changes: 15 additions & 2 deletions src/LayoutStrategy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as fs from "fs-extra";
import { NotionPage } from "./NotionPage";

// Here a fuller name would be File Tree Layout Strategy. That is,
Expand All @@ -22,7 +21,21 @@ export abstract class LayoutStrategy {
extensionWithDot: string
): string;

public getLinkPathForPage(page: NotionPage): string {
public getLinkPathForPage(targetPage: { page?: NotionPage; tab?: string }): string {
let tab = "";
if (targetPage.tab !== undefined) {
tab = targetPage.tab;
}
if (targetPage.page && targetPage.page.slug.startsWith("/")) {
return (tab + targetPage.page.slug);
} else if (targetPage.page) {
return targetPage.page.layoutContext.toLocaleLowerCase() + "/" + targetPage.page.slug;
} else {
return "";
}
}

public getPageSlug(page: NotionPage): string {
// the url we return starts with a "/", meaning it is relative to the root of the markdown root (e.g. /docs root in Docusaurus)
return ("/" + page.slug).replaceAll("//", "/");
}
Expand Down
5 changes: 2 additions & 3 deletions src/NotionPage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,11 @@ export class NotionPage {

// In Notion, pages from the Outline have "title"'s.
private get title(): string {
return this.getPlainTextProperty("title", "title missing");
return this.getPlainTextProperty("title", "title missing").trim().replace(/\s{2,}/g, ' ');
}
// In Notion, pages from the Database have "Name"s.
private get name(): string {
return this.getPlainTextProperty("Name", "name missing");
return this.getPlainTextProperty("Name", "name missing").trim().replace(/\s{2,}/g, ' ');
}

private explicitSlug(): string | undefined {
Expand All @@ -132,7 +132,6 @@ export class NotionPage {
.replace(/^\//, "")
.replaceAll(" ", "-")
.replaceAll(/[^a-z0-9-]/g, "") // remove special characters and punctuation
.replaceAll("--", "-") // remove consecutive dashes
)
);
}
Expand Down
38 changes: 22 additions & 16 deletions src/plugins/internalLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function convertInternalUrl(context: IDocuNotionContext, url: string): st
}

if (targetPage.page && targetPage.tab) {
return convertLinkHref(context, targetPage.tab, targetPage.page, url);
return convertLinkHref(context, targetPage, url);
} else {
warning(`Could not find the target of this link. Links to outline sections are not supported. ${url}.`);
return `${id}[broken link]`;
Expand Down Expand Up @@ -69,7 +69,7 @@ function convertInternalLink(context: IDocuNotionContext, markdownLink: string):

if (targetPage.page && targetPage.tab) {
const label = convertLinkLabel(targetPage.page, labelFromNotion);
const url = convertLinkHref(context, targetPage.tab, targetPage.page, hrefFromNotion);
const url = convertLinkHref(context, targetPage, hrefFromNotion);
return `[${label}](${url})`;
} else {
warning(`Could not find a local target for ${hrefFromNotion}.`);
Expand All @@ -90,21 +90,27 @@ function convertLinkLabel(targetPage: NotionPage, text: string): string {
* Note: The official Notion API does not include links to headings unless they are part of an inline link.
*/
function convertLinkHref(
context: IDocuNotionContext,
tab: string,
page: NotionPage,
url: string
context: IDocuNotionContext,
targetPage: { page?: NotionPage; tab?: string },
url: string
): string {
let convertedLink = "/" + tab + context.layoutStrategy.getLinkPathForPage(page);

// Extract the fragment identifier from the URL, if it exists
const { fragmentId } = parseLinkId(url);
if (fragmentId !== "") {
verbose(`[InternalLinkPlugin] Extracted Fragment ID from ${url}: ${fragmentId}`);
}
convertedLink += fragmentId;

return convertedLink;
if (!targetPage.page || !targetPage.tab) {
warning(`Could not find the target of this link. Links to outline sections are not supported. ${url}.`);
return `${url}[broken link]`;
}
else {
let convertedLink = context.layoutStrategy.getLinkPathForPage(targetPage);
verbose(`>>>>>>>> ${convertedLink}`);

// Extract the fragment identifier from the URL, if it exists
const { fragmentId } = parseLinkId(url);
if (fragmentId !== "") {
verbose(`[InternalLinkPlugin] Extracted Fragment ID from ${url}: ${fragmentId}`);
}
convertedLink += fragmentId;

return convertedLink;
}
}


Expand Down
2 changes: 1 addition & 1 deletion src/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ async function outputPages(
for (const page of tabPages) {
const mdPath = layoutStrategy.getPathForPage(page, ".mdx");
context.directoryContainingMarkdown = Path.dirname(mdPath);
context.relativeFilePathToFolderContainingPage = Path.dirname(layoutStrategy.getLinkPathForPage(page));
context.relativeFilePathToFolderContainingPage = Path.dirname(layoutStrategy.getPageSlug(page));

if (page.type === PageType.DatabasePage && context.options.statusTag != "*" && page.status !== context.options.statusTag) {
verbose(`Skipping page because status is not '${context.options.statusTag}': ${page.nameOrTitle}`);
Expand Down

0 comments on commit 7163f8b

Please sign in to comment.