Skip to content

Commit

Permalink
Modifies url encoding to target only the filename and skip spaces (#1322
Browse files Browse the repository at this point in the history
)
  • Loading branch information
MABruni committed Jan 15, 2024
1 parent cdbb965 commit 2fba6e9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 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 encode spaces links', async () => {
it('should put links with spaces in angel brackets', async () => {
const note = findBySlug('angel-reference');
const expected = {
newText: textForNote(
`
[//begin]: # "Autogenerated link references for markdown compatibility"
[Note being referred as angel]: Note%20being%20referred%20as%20angel "Note being referred as angel"
[Note being referred as angel]: <Note being referred as angel> "Note being referred as angel"
[//end]: # "Autogenerated link references"`
),
range: Range.create(3, 0, 3, 0),
Expand Down
8 changes: 7 additions & 1 deletion packages/foam-vscode/src/core/services/markdown-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ export function createMarkdownReferences(
relativeUri = relativeUri.changeExtension('*', '');
}

// Extract base path and link name separately.
const basePath = relativeUri.path.split('/').slice(0, -1).join('/');
const linkName = relativeUri.path.split('/').pop();

const encodedURL = encodeURIComponent(linkName).replace(/%20/g, ' ');

// [wikilink-text]: path/to/file.md "Page title"
return {
// embedded looks like ![[note-a]]
Expand All @@ -145,7 +151,7 @@ export function createMarkdownReferences(
link.isEmbed ? 3 : 2,
link.rawText.length - 2
),
url: encodeURIComponent(relativeUri.path),
url: `${basePath ? basePath + '/' : ''}${encodedURL}`,
title: target.title,
};
})
Expand Down

0 comments on commit 2fba6e9

Please sign in to comment.