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

Use glob expressions to filter files and folders #3335

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/common/filesystem/paths.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs'
import path from 'path'
import { isFile, isFile2, isSymbolicLink } from './index'
import minimatch from 'minimatch'

const isOsx = process.platform === 'darwin'

Expand Down Expand Up @@ -119,3 +120,19 @@ export const getResourcesPath = () => {
}
return resPath
}

/**
* Returns true if the pathname matches one of the exclude patterns.
*
* @param {string} pathname Path of file or directory
* @param {string} patterns Glob expressions
*/
export const checkPathExcludePattern = (pathname, patterns) => {
if (!pathname || typeof pathname !== 'string') return false
for (const pattern of patterns) {
if (minimatch(pathname, pattern, { matchBase: true })) {
return true
}
}
return false
}
6 changes: 5 additions & 1 deletion src/main/filesystem/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fsPromises from 'fs/promises'
import log from 'electron-log'
import chokidar from 'chokidar'
import { exists } from 'common/filesystem'
import { hasMarkdownExtension } from 'common/filesystem/paths'
import { hasMarkdownExtension, checkPathExcludePattern } from 'common/filesystem/paths'
import { getUniqueId } from '../utils'
import { loadMarkdownFile } from '../filesystem/markdown'
import { isLinux, isOsx } from '../config'
Expand Down Expand Up @@ -159,6 +159,10 @@ class Watcher {
if (/(?:^|[/\\])(?:\..|node_modules|(?:.+\.asar))/.test(pathname)) {
return true
}

if (checkPathExcludePattern(pathname, this._preferences.getItem('treePathExcludePatterns'))) {
return true
}
if (fileInfo.isDirectory()) {
return false
}
Expand Down
13 changes: 12 additions & 1 deletion src/renderer/prefComponents/general/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@
:onChange="value => onSelectChange('wordWrapInToc', value)"
></bool>

<text-box description='Patterns to exclude directorys or files'
notes='Glob Patterns, Use "," to separate multiple pattern. Requires restart.'
:input="projectPaths"
:defaultValue="treePathExcludePatterns"
:onChange="value => onSelectChange('treePathExcludePatterns', value.split(','))"
more="https://github.com/isaacs/minimatch"
></text-box>

<!-- TODO: The description is very bad and the entry isn't used by the editor. -->
<cur-select
description="Sort field for files in open folders"
Expand Down Expand Up @@ -125,6 +133,7 @@ import Range from '../common/range'
import CurSelect from '../common/select'
import Bool from '../common/bool'
import Separator from '../common/separator'
import textBox from '../common/textBox'
import { isOsx } from '@/util'

import {
Expand All @@ -140,7 +149,8 @@ export default {
Bool,
Range,
CurSelect,
Separator
Separator,
textBox
},
data () {
this.titleBarStyleOptions = titleBarStyleOptions
Expand All @@ -158,6 +168,7 @@ export default {
defaultDirectoryToOpen: state => state.preferences.defaultDirectoryToOpen,
openFilesInNewWindow: state => state.preferences.openFilesInNewWindow,
openFolderInNewWindow: state => state.preferences.openFolderInNewWindow,
projectPaths: state => state.preferences.treePathExcludePatterns,
zoom: state => state.preferences.zoom,
hideScrollbar: state => state.preferences.hideScrollbar,
wordWrapInToc: state => state.preferences.wordWrapInToc,
Expand Down
1 change: 1 addition & 0 deletions src/renderer/store/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const state = {
fileSortBy: 'created',
startUpAction: 'lastState',
defaultDirectoryToOpen: '',
treePathExcludePatterns: [],
language: 'en',

editorFontFamily: 'Open Sans',
Expand Down
1 change: 1 addition & 0 deletions static/preference.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"fileSortBy": "created",
"startUpAction": "lastState",
"defaultDirectoryToOpen": "",
"treePathExcludePatterns": [],
"language": "en",

"editorFontFamily": "Open Sans",
Expand Down