Skip to content

Commit

Permalink
Add and use parseRef utility
Browse files Browse the repository at this point in the history
  • Loading branch information
svsool committed Jul 21, 2020
1 parent 8a2e01e commit 0266b25
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/commands/openDocumentByReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import {
getWorkspaceCache,
findUriByRef,
ensureDirectoryExistence,
parseRef,
} from '../utils';

const openDocumentByReference = async ({ reference }: { reference: string }) => {
const [ref] = reference.split('|');
const { ref } = parseRef(reference);

const uri = findUriByRef(getWorkspaceCache().allUris, ref);

Expand Down
29 changes: 15 additions & 14 deletions src/extensions/extendMarkdownIt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
containsImageExt,
findUriByRef,
extractEmbedRefs,
parseRef,
} from '../utils';

const getInvalidRefAnchor = (text: string) =>
Expand All @@ -25,23 +26,23 @@ const extendMarkdownIt = (md: MarkdownIt) => {
.use(markdownItRegex, {
name: 'ref-resource',
regex: /!\[\[([^\[\]]+?)\]\]/,
replace: (ref: string) => {
const [refStr, label = ''] = ref.split('|');
replace: (rawRef: string) => {
const { ref, label } = parseRef(rawRef);

if (containsImageExt(refStr)) {
const imagePath = findUriByRef(getWorkspaceCache().imageUris, refStr)?.fsPath;
if (containsImageExt(ref)) {
const imagePath = findUriByRef(getWorkspaceCache().imageUris, ref)?.fsPath;

if (imagePath) {
return `<div><img src="${getImgUrlForMarkdownPreview(imagePath)}" alt="${
label || refStr
label || ref
}" /></div>`;
}
}

const fsPath = findUriByRef(getWorkspaceCache().markdownUris, refStr)?.fsPath;
const fsPath = findUriByRef(getWorkspaceCache().markdownUris, ref)?.fsPath;

if (!fsPath) {
return getInvalidRefAnchor(label || refStr);
return getInvalidRefAnchor(label || ref);
}

const name = path.parse(fsPath).name;
Expand All @@ -51,10 +52,10 @@ const extendMarkdownIt = (md: MarkdownIt) => {
const refs = extractEmbedRefs(content).map((ref) => ref.toLowerCase());

const cyclicLinkDetected =
refs.includes(refStr.toLowerCase()) || refs.some((ref) => refsStack.includes(ref));
refs.includes(ref.toLowerCase()) || refs.some((ref) => refsStack.includes(ref));

if (!cyclicLinkDetected) {
refsStack.push(refStr.toLowerCase());
refsStack.push(ref.toLowerCase());
}

const html = `<div class="memo-markdown-embed">
Expand Down Expand Up @@ -83,16 +84,16 @@ const extendMarkdownIt = (md: MarkdownIt) => {
.use(markdownItRegex, {
name: 'ref-document',
regex: /\[\[([^\[\]]+?)\]\]/,
replace: (ref: string) => {
const [refStr, label = ''] = ref.split('|');
replace: (rawRef: string) => {
const { ref, label } = parseRef(rawRef);

const fsPath = findUriByRef(getWorkspaceCache().allUris, refStr)?.fsPath;
const fsPath = findUriByRef(getWorkspaceCache().allUris, ref)?.fsPath;

if (!fsPath) {
return getInvalidRefAnchor(label || refStr);
return getInvalidRefAnchor(label || ref);
}

return getRefAnchor(getFileUrlForMarkdownPreview(fsPath), label || refStr);
return getRefAnchor(getFileUrlForMarkdownPreview(fsPath), label || ref);
},
});

Expand Down
19 changes: 19 additions & 0 deletions src/utils/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
cacheWorkspace,
cleanWorkspaceCache,
getRefUriUnderCursor,
parseRef,
} from './utils';

describe('containsImageExt()', () => {
Expand Down Expand Up @@ -230,3 +231,21 @@ describe('getRefUriUnderCursor()', () => {
expect(getRefUriUnderCursor()).toBeNull();
});
});

describe('parseRef()', () => {
it('should fail on providing wrong parameter type', () => {
expect(() => parseRef((undefined as unknown) as string)).toThrowError();
});

it('should return empty ref and label', () => {
expect(parseRef('')).toEqual({ ref: '', label: '' });
});

it('should parse raw ref and return ref and label', () => {
expect(parseRef('link|Label')).toEqual({ ref: 'link', label: 'Label' });
});

it('should favour only first divider', () => {
expect(parseRef('link|||Label')).toEqual({ ref: 'link', label: '||Label' });
});
});
18 changes: 12 additions & 6 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,9 @@ export const getReferenceAtPosition = (
return null;
}

const [ref, label = ''] = document
.getText(range)
.replace('![[', '')
.replace('[[', '')
.replace(']]', '')
.split('|');
const { ref, label } = parseRef(
document.getText(range).replace('![[', '').replace('[[', '').replace(']]', ''),
);

return {
ref,
Expand Down Expand Up @@ -268,3 +265,12 @@ export const getRefUriUnderCursor = (): vscode.Uri | null | undefined => {

return refResult && findUriByRef(getWorkspaceCache().allUris, refResult.ref);
};

export const parseRef = (rawRef: string): RefT => {
const dividerPosition = rawRef.indexOf('|');

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

0 comments on commit 0266b25

Please sign in to comment.