Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a configuration option for other file extensions #621

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,39 @@
"description": "Whether to enable links completion in the editor. Reload required!",
"type": "boolean"
},
"memo.links.extensions.other": {
svsool marked this conversation as resolved.
Show resolved Hide resolved
"scope": "resource",
"description": "Other file extensions supported by links. Use a single entry of '*' to include all extensions.",
"default": [
"doc",
"docx",
"rtf",
"txt",
"odt",
"xls",
"xlsx",
"ppt",
"pptm",
"pptx",
"pdf",
"pages",
"mp4",
"mov",
"wmv",
"flv",
"avi",
"mkv",
"mp3",
"webm",
"wav",
"m4a",
"ogg",
"3gp",
"flac",
"msg"
],
"type": "array"
},
"memo.links.following.enabled": {
"default": true,
"scope": "resource",
Expand Down Expand Up @@ -169,8 +202,7 @@
"scope": "resource",
"description": "Whether to enhance built-in VSCode Markdown preview with links highlight, navigation and resource embedding. Reload required!",
"type": "boolean"
}
}
} }
},
"keybindings": [
{
Expand Down
17 changes: 17 additions & 0 deletions src/utils/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1181,6 +1181,23 @@ describe('findNonIgnoredFiles()', () => {
});
});

describe('when config includes py files', () => {
it('should find non-ignored files', async () => {
const prevConfig = getConfigProperty('memo.links.extensions.other', []);
await updateConfigProperty('memo.links.extensions.other', ['py']);
const allowedName = rndName();

await createFile(`${allowedName}.py`);

const files = await findNonIgnoredFiles('**/*.py');

expect(files).toHaveLength(1);
expect(path.basename(files[0].fsPath)).toBe(`${allowedName}.py`);

await updateConfigProperty('memo.links.extensions.other', prevConfig);
});
});

describe('when exclude param passed explicitly and search.exclude set', () => {
it('should find non-ignored files', async () => {
const prevConfig = getConfigProperty('search.exclude', {});
Expand Down
66 changes: 37 additions & 29 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,38 @@ export const imageExts = ['png', 'jpg', 'jpeg', 'svg', 'gif', 'webp'];

const imageExtsRegex = new RegExp(`.(${imageExts.join('|')})$`, 'i');

export const otherExts = [
export function getMemoConfigProperty(
property: 'links.format',
fallback: 'short',
): 'short' | 'long';

export function getMemoConfigProperty(
property: 'backlinksPanel.collapseParentItems',
fallback: null | boolean,
): boolean;

export function getMemoConfigProperty(
property: 'links.preview.imageMaxHeight',
fallback: null | number,
): number;

export function getMemoConfigProperty(
property: 'links.rules',
fallback: null | Array<LinkRuleT>,
): LinkRuleT[];

export function getMemoConfigProperty(property: MemoBoolConfigProp, fallback: boolean): boolean;

export function getMemoConfigProperty(
property: 'links.extensions.other',
fallback: null | Array<String>,
): String[];

export function getMemoConfigProperty<T>(property: string, fallback: T): T {
return getConfigProperty(`memo.${property}`, fallback);
}

export const otherExts = getMemoConfigProperty('links.extensions.other', [
'doc',
'docx',
'rtf',
Expand All @@ -39,9 +70,12 @@ export const otherExts = [
'3gp',
'flac',
'msg',
];
]);

const otherExtsRegex = new RegExp(`.(${otherExts.join('|')})$`, 'i');
export const otherExtsRegex =
otherExts.length == 1 && otherExts[0] == '*'
? new RegExp(`.*`, 'i')
: new RegExp(`.(${otherExts.join('|')})$`, 'i');

// Remember to edit accordingly when extensions above edited
export const commonExtsHint =
Expand Down Expand Up @@ -160,32 +194,6 @@ export type MemoBoolConfigProp =
| 'backlinksPanel.enabled'
| 'markdownPreview.enabled';

export function getMemoConfigProperty(
property: 'links.format',
fallback: 'short',
): 'short' | 'long';

export function getMemoConfigProperty(
property: 'backlinksPanel.collapseParentItems',
fallback: null | boolean,
): boolean;

export function getMemoConfigProperty(
property: 'links.preview.imageMaxHeight',
fallback: null | number,
): number;

export function getMemoConfigProperty(
property: 'links.rules',
fallback: null | Array<LinkRuleT>,
): LinkRuleT[];

export function getMemoConfigProperty(property: MemoBoolConfigProp, fallback: boolean): boolean;

export function getMemoConfigProperty<T>(property: string, fallback: T): T {
return getConfigProperty(`memo.${property}`, fallback);
}

export const matchAll = (pattern: RegExp, text: string): Array<RegExpMatchArray> => {
let match: RegExpMatchArray | null;
const out: RegExpMatchArray[] = [];
Expand Down
5 changes: 4 additions & 1 deletion src/workspace/cache/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ export const cacheUris = async () => {
const { findNonIgnoredFiles, imageExts, otherExts } = utils();
const markdownUris = await findNonIgnoredFiles('**/*.md');
const imageUris = await findNonIgnoredFiles(`**/*.{${imageExts.join(',')}}`);
const otherUris = await findNonIgnoredFiles(`**/*.{${otherExts.join(',')}}`);
const otherUris =
otherExts.length == 1 && otherExts[0] == '*'
? await findNonIgnoredFiles('**/*')
: await findNonIgnoredFiles(`**/*.{${otherExts.join(',')}}`);

workspaceCache.markdownUris = sortPaths(markdownUris, { pathKey: 'path', shallowFirst: true });
workspaceCache.imageUris = sortPaths(imageUris, { pathKey: 'path', shallowFirst: true });
Expand Down