Skip to content

Commit

Permalink
fixed internal links
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscoMoretti committed Sep 1, 2024
1 parent 59c4bc1 commit b7f9277
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
14 changes: 14 additions & 0 deletions packages/download-notion/src/pathUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import path from "path"

export function addPathPrefix(basePath: string, prefix: string): string {
if (prefix === "") {
return basePath
}
const normalizedPrefix = path.normalize(prefix)
const normalizedPath = path.normalize(basePath)

Expand All @@ -12,6 +15,10 @@ export function addPathPrefix(basePath: string, prefix: string): string {
}

export function removePathPrefix(fullPath: string, prefix: string): string {
if (prefix === "") {
return fullPath
}

const normalizedFullPath = path.normalize(fullPath)
const normalizedPrefix = path.normalize(prefix)

Expand All @@ -33,3 +40,10 @@ export function removePathPrefix(fullPath: string, prefix: string): string {

return result
}

export function removePathExtension(convertedLink: string) {
// Returns a normalized path with the extension removed
const parsedPath = path.parse(convertedLink)
convertedLink = path.join(parsedPath.dir, parsedPath.name)
return convertedLink
}
7 changes: 4 additions & 3 deletions packages/download-notion/src/plugins/internalLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from "path"

import { NotionPage } from "../NotionPage"
import { error, warning } from "../log"
import { removePathExtension } from "../pathUtils"
import { IDocuNotionContext, IPlugin } from "./pluginTypes"

// converts a url to a local link, if it is a link to a page in the Notion site
Expand Down Expand Up @@ -88,14 +89,13 @@ function convertLinkHref(
targetPage: NotionPage,
url: string
): string {
// TODO: Instead of type directory this should be type `markdown`
let convertedLink = context.filesManager.get(
"base",
"markdown",
"page",
targetPage.id
)?.path
if (!context.options.pageLinkHasExtension) {
convertedLink = path.basename(convertedLink, path.extname(convertedLink))
convertedLink = removePathExtension(convertedLink)
}

/*****************************
Expand All @@ -111,6 +111,7 @@ function convertLinkHref(
//verbose(`Converting Link ${url} --> ${convertedLink}`);
return convertedLink
}

// Parse the link ID to get the base (before the #) and the fragment (# and after).
export function parseLinkId(fullLinkId: string): {
baseLinkId: string // before the #
Expand Down
8 changes: 2 additions & 6 deletions packages/download-notion/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import chalk from "chalk"
import { NotionPage } from "./NotionPage"
import { IDocuNotionConfig } from "./config/configuration"
import { error, info, logDebug, logDebugFn, verbose, warning } from "./log"
import { getImageUrl } from "./notion_objects_utils"
import {
IDocuNotionContext,
IRegexMarkdownModification,
Expand Down Expand Up @@ -258,13 +259,8 @@ function getFrontMatter(page: NotionPage): string {

const standardProperties = {
title: `${page.title.replaceAll(":", "-")}`,
// TODO: Consider clearing the props above which are non standard
id: page.metadata.id,
// TODO: Cover images have to be handled, downloaded and stored. Decide if only store files, or also external.
cover:
page.metadata.cover?.external?.url ||
page.metadata.cover?.file?.url ||
"",
cover: page.metadata.cover ? getImageUrl(page.metadata.cover) : "",
created_time: page.metadata.created_time,
last_edited_time: page.metadata.last_edited_time,
}
Expand Down
32 changes: 31 additions & 1 deletion packages/download-notion/test/pathUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import path from "path"
import { describe, expect, test } from "vitest"

import { addPathPrefix, removePathPrefix } from "../src/pathUtils"
import {
addPathPrefix,
removePathExtension,
removePathPrefix,
} from "../src/pathUtils"

describe("addPathPrefix", () => {
test("adds prefix to relative path", () => {
Expand All @@ -10,6 +14,10 @@ describe("addPathPrefix", () => {
)
})

test("no prefix maintains the original path", () => {
expect(addPathPrefix("/foo/bar", "")).toBe(path.normalize("/foo/bar"))
})

test("modifies absolute path", () => {
expect(addPathPrefix("/foo/bar", "/prefix")).toBe(
path.normalize("/prefix/foo/bar")
Expand Down Expand Up @@ -52,3 +60,25 @@ describe("removePathPrefix", () => {
)
})
})

describe("removePathExtension", () => {
test("removes extension from file path", () => {
expect(removePathExtension("/foo/bar.txt")).toBe("/foo/bar")
})

test("handles path without extension", () => {
expect(removePathExtension("/foo/bar")).toBe("/foo/bar")
})

test("handles path with multiple dots", () => {
expect(removePathExtension("/foo/bar.test.js")).toBe("/foo/bar.test")
})

test("handles relative paths", () => {
expect(removePathExtension("./foo/bar.md")).toBe("foo/bar")
})

test("handles paths with only filename", () => {
expect(removePathExtension("file.txt")).toBe("file")
})
})

0 comments on commit b7f9277

Please sign in to comment.