Skip to content

Commit

Permalink
fix: Properly handle escape symbol for piped links (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
svsool committed Oct 31, 2020
1 parent 172d405 commit df899d1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/features/ReferenceHoverProvider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,26 @@ describe('ReferenceHoverProvider', () => {
],
});
});

it('should provide hover for a link with escape symbol for the label', async () => {
const name0 = rndName();
const name1 = rndName();

await createFile(`${name0}.md`, `[[${name1}\\|Label]]`);
await createFile(`${name1}.md`, '# Hello world');

const doc = await openTextDocument(`${name0}.md`);

const referenceHoverProvider = new ReferenceHoverProvider();

expect(
toPlainObject(referenceHoverProvider.provideHover(doc, new vscode.Position(0, 4))),
).toEqual({
contents: ['# Hello world'],
range: [
{ character: expect.any(Number), line: 0 },
{ character: expect.any(Number), line: 0 },
],
});
});
});
16 changes: 16 additions & 0 deletions src/features/extendMarkdownIt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,20 @@ describe('extendMarkdownIt feature', () => {
"
`);
});

it('should render html link to the existing note with escape symbol for the label', async () => {
const name = rndName();

await createFile(`${name}.md`);

const md = extendMarkdownIt(MarkdownIt());

const notePath = `${path.join(getWorkspaceFolder()!, `${name}.md`)}`;

const url = getFileUrlForMarkdownPreview(notePath);

expect(md.render(`[[${name}\\|Test Label]]`)).toBe(
`<p><a title="${url}" href="${url}">Test Label</a></p>\n`,
);
});
});
4 changes: 4 additions & 0 deletions src/utils/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,10 @@ describe('parseRef()', () => {
it('should favour only first divider', () => {
expect(parseRef('link|||Label')).toEqual({ ref: 'link', label: '||Label' });
});

it('should ignore escape symbol', () => {
expect(parseRef('link\\|Label')).toEqual({ ref: 'link', label: 'Label' });
});
});

describe('replaceRefs()', () => {
Expand Down
9 changes: 7 additions & 2 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,11 +436,16 @@ export const getRefUriUnderCursor = (): vscode.Uri | null | undefined => {
};

export const parseRef = (rawRef: string): RefT => {
const dividerPosition = rawRef.indexOf('|');
const escapedDividerPosition = rawRef.indexOf('\\|');
const dividerPosition =
escapedDividerPosition !== -1 ? escapedDividerPosition : rawRef.indexOf('|');

return {
ref: dividerPosition !== -1 ? rawRef.slice(0, dividerPosition) : rawRef,
label: dividerPosition !== -1 ? rawRef.slice(dividerPosition + 1, rawRef.length) : '',
label:
dividerPosition !== -1
? rawRef.slice(dividerPosition + (escapedDividerPosition !== -1 ? 2 : 1), rawRef.length)
: '',
};
};

Expand Down

0 comments on commit df899d1

Please # to comment.