Skip to content

Commit

Permalink
[ #70, Link improvements ] (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamedsalem401 authored Dec 1, 2023
1 parent ca05a77 commit 22dcad9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
12 changes: 9 additions & 3 deletions src/lib/parseFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ export const extractWikiLinks = (ast: Root, options?: ParsingOptions) => {
const extractors: LinkExtractors = {
link: (node: any) => {
const to = !node.url.startsWith("http")
? path.posix.join(directory, node.url)
? node.url.startsWith("/")
? node.url.slice(1)
: path.posix.join(directory, node.url)
: node.url;
return {
from: from,
Expand All @@ -111,7 +113,9 @@ export const extractWikiLinks = (ast: Root, options?: ParsingOptions) => {
},
image: (node: any) => ({
from: from,
to: path.posix.join(directory, node.url),
to: node.url.startsWith("/")
? node.url.slice(1)
: path.posix.join(directory, node.url),
toRaw: node.url,
text: node.alt || "",
embed: true,
Expand All @@ -133,7 +137,9 @@ export const extractWikiLinks = (ast: Root, options?: ParsingOptions) => {
text = node.children?.[0]?.value || "";
}
const to = !linkSrc.startsWith("http")
? path.posix.join(directory, linkSrc)
? linkSrc.startsWith("/")
? linkSrc.slice(1)
: path.posix.join(directory, linkSrc)
: linkSrc;

return {
Expand Down
27 changes: 21 additions & 6 deletions src/tests/extractWikiLinks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,28 @@ describe("extractWikiLinks", () => {
expect(links).toEqual(expectedLinks);
});

test("should extract CommonMark links with relative path", () => {
const links = getLinksFromSource("[hello](./world.md)");
const expectedLinks = [
{
from: "abc/foobar.md",
to: "abc/world.md",
toRaw: "./world.md",
text: "hello",
embed: false,
internal: true,
},
];
expect(links).toEqual(expectedLinks);
});

test("should extract CommonMark links with absolute path", () => {
const links = getLinksFromSource("[hello](/world)");
const links = getLinksFromSource("[hello](/world.md)");
const expectedLinks = [
{
from: "abc/foobar.md",
to: "abc/world",
toRaw: "/world",
to: "world.md",
toRaw: "/world.md",
text: "hello",
embed: false,
internal: true,
Expand Down Expand Up @@ -158,23 +173,23 @@ describe("extractWikiLinks", () => {
from: "abc/foobar.md",
internal: true,
text: "",
to: "abc/some/folder/Page 1",
to: "some/folder/Page 1",
toRaw: "/some/folder/Page 1",
},
{
embed: false,
from: "abc/foobar.md",
internal: true,
text: "",
to: "abc/some/folder/Page 2",
to: "some/folder/Page 2",
toRaw: "/some/folder/Page 2",
},
{
embed: false,
from: "abc/foobar.md",
internal: true,
text: "",
to: "abc/some/folder/Page 3",
to: "some/folder/Page 3",
toRaw: "/some/folder/Page 3",
},
];
Expand Down

0 comments on commit 22dcad9

Please sign in to comment.