Skip to content

Commit

Permalink
Fix foambubble#1298. Wikilink definitions links not working. (foambub…
Browse files Browse the repository at this point in the history
…ble#1311)

* Added encoding to handle special characters when wikilink definitions are generated

* Added tests for wikilinks encoding and updated previous tests to support encoded links
  • Loading branch information
MABruni committed Dec 13, 2023
1 parent 8bd679c commit 4d99883
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,13 @@ describe('generateLinkReferences', () => {
expect(actual).toEqual(expected);
});

it('should put links with spaces in angel brackets', async () => {
it('should encode spaces links', async () => {
const note = findBySlug('angel-reference');
const expected = {
newText: textForNote(
`
[//begin]: # "Autogenerated link references for markdown compatibility"
[Note being referred as angel]: <Note being referred as angel> "Note being referred as angel"
[Note being referred as angel]: Note%20being%20referred%20as%20angel "Note being referred as angel"
[//end]: # "Autogenerated link references"`
),
range: Range.create(3, 0, 3, 0),
Expand Down
98 changes: 95 additions & 3 deletions packages/foam-vscode/src/core/services/markdown-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,65 @@ describe('Link resolution', () => {
noteA.uri.withFragment('section')
);
});

it('should resolve wikilinks with special characters', () => {
const ws = createTestWorkspace();
const noteA = createNoteFromMarkdown(
`Link to [[page: a]] and [[page %b%]] and [[page? c]] and [[[page] d]] and
[[page ^e^]] and [[page \`f\`]] and [[page {g}]] and [[page ~i]] and
[[page /j]]`
);
const noteB = createNoteFromMarkdown(
'Note containing :',
'/dir1/page: a.md'
);
const noteC = createNoteFromMarkdown(
'Note containing %',
'/dir1/page %b%.md'
);
const noteD = createNoteFromMarkdown(
'Note containing ?',
'/dir1/page? c.md'
);
const noteE = createNoteFromMarkdown(
'Note containing ]',
'/dir1/[page] d.md'
);
const noteF = createNoteFromMarkdown(
'Note containing ^',
'/dir1/page ^e^.md'
);
const noteG = createNoteFromMarkdown(
'Note containing `',
'/dir1/page `f`.md'
);
const noteH = createNoteFromMarkdown(
'Note containing { and }',
'/dir1/page {g}.md'
);
const noteI = createNoteFromMarkdown(
'Note containing ~',
'/dir1/page ~i.md'
);
ws.set(noteA)
.set(noteB)
.set(noteC)
.set(noteD)
.set(noteE)
.set(noteF)
.set(noteG)
.set(noteH)
.set(noteI);

expect(ws.resolveLink(noteA, noteA.links[0])).toEqual(noteB.uri);
expect(ws.resolveLink(noteA, noteA.links[1])).toEqual(noteC.uri);
expect(ws.resolveLink(noteA, noteA.links[2])).toEqual(noteD.uri);
expect(ws.resolveLink(noteA, noteA.links[3])).toEqual(noteE.uri);
expect(ws.resolveLink(noteA, noteA.links[4])).toEqual(noteF.uri);
expect(ws.resolveLink(noteA, noteA.links[5])).toEqual(noteG.uri);
expect(ws.resolveLink(noteA, noteA.links[6])).toEqual(noteH.uri);
expect(ws.resolveLink(noteA, noteA.links[7])).toEqual(noteI.uri);
});
});

describe('Markdown direct links', () => {
Expand Down Expand Up @@ -311,7 +370,7 @@ describe('Generation of markdown references', () => {
.set(createNoteFromMarkdown('Content of note C', '/dir3/page-c.md'));

const references = createMarkdownReferences(workspace, noteA.uri, true);
expect(references.map(r => r.url)).toEqual([
expect(references.map(r => decodeURIComponent(r.url))).toEqual([
'../dir2/page-b.md',
'../dir3/page-c.md',
]);
Expand All @@ -329,7 +388,7 @@ describe('Generation of markdown references', () => {
.set(createNoteFromMarkdown('Content of note C', '/dir3/page-c.md'));

const references = createMarkdownReferences(workspace, noteA.uri, true);
expect(references.map(r => [r.url, r.label])).toEqual([
expect(references.map(r => [decodeURIComponent(r.url), r.label])).toEqual([
['../dir2/page-b.md', 'page-b'],
['../dir3/page-c.md', 'page-c'],
]);
Expand All @@ -347,9 +406,42 @@ describe('Generation of markdown references', () => {
.set(createNoteFromMarkdown('Content of note C', '/dir3/page-c.md'));

const references = createMarkdownReferences(workspace, noteA.uri, true);
expect(references.map(r => r.url)).toEqual([
expect(references.map(r => decodeURIComponent(r.url))).toEqual([
'../dir2/page-b.md',
'../dir3/page-c.md',
]);
});

it('should encode special characters in links', () => {
const workspace = createTestWorkspace();
const noteA = createNoteFromMarkdown(
`Link to [[page: a]] and [[page %b%]] and [[page? c]] and [[[page] d]] and
[[page ^e^]] and [[page \`f\`]] and [[page {g}]] and [[page ~i]] and
[[page /j]]`
);
workspace
.set(noteA)
.set(createNoteFromMarkdown('Note containing :', '/dir1/page: a.md'))
.set(createNoteFromMarkdown('Note containing %', '/dir1/page %b%.md'))
.set(createNoteFromMarkdown('Note containing ?', '/dir1/page? c.md'))
.set(createNoteFromMarkdown('Note containing ]', '/dir1/[page] d.md'))
.set(createNoteFromMarkdown('Note containing ^', '/dir1/page ^e^.md'))
.set(createNoteFromMarkdown('Note containing `', '/dir1/page `f`.md'))
.set(
createNoteFromMarkdown('Note containing { and }', '/dir1/page {g}.md')
)
.set(createNoteFromMarkdown('Note containing ~', '/dir1/page ~i.md'));

const references = createMarkdownReferences(workspace, noteA.uri, true);
expect(references.map(r => decodeURIComponent(r.url))).toEqual([
'../dir1/page: a.md',
'../dir1/page %b%.md',
'../dir1/page? c.md',
'../dir1/[page] d.md',
'../dir1/page ^e^.md',
'../dir1/page `f`.md',
'../dir1/page {g}.md',
'../dir1/page ~i.md',
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ export function createMarkdownReferences(
link.isEmbed ? 3 : 2,
link.rawText.length - 2
),
url: relativeUri.path,
url: encodeURIComponent(relativeUri.path),
title: target.title,
};
})
Expand Down

0 comments on commit 4d99883

Please sign in to comment.