Fix #1879 Add .md extension to files created in sidebar if no extension is given (#1886)

* Fix #1879: Add .md extension to files created in sidebar if no extension given.

* Use hasMarkdownExtension function everywhere an extension has to be checked.

Allowed markdown extensions are handeled in several places in different ways. Use the hasMarkdownFunction from common/filesystem/paths.js to allow to easily update the allowed extensions when needed.
This commit is contained in:
Davis Riedel 2020-02-08 10:26:37 +01:00 committed by GitHub
parent 622ff90d58
commit 862324387c
4 changed files with 14 additions and 5 deletions

View File

@ -15,6 +15,8 @@ export const MARKDOWN_EXTENSIONS = [
'txt' 'txt'
] ]
export const MARKDOWN_INCLUSIONS = MARKDOWN_EXTENSIONS.map(x => '*.' + x)
export const IMAGE_EXTENSIONS = [ export const IMAGE_EXTENSIONS = [
'jpeg', 'jpeg',
'jpg', 'jpg',

View File

@ -1,11 +1,10 @@
import path from 'path' import path from 'path'
import { ipcRenderer } from 'electron' import { ipcRenderer } from 'electron'
import { isChildOfDirectory } from 'common/filesystem/paths' import { isChildOfDirectory, hasMarkdownExtension, MARKDOWN_INCLUSIONS } from '../../common/filesystem/paths'
import bus from '../bus' import bus from '../bus'
import { delay } from '@/util' import { delay } from '@/util'
import FileSearcher from '@/node/fileSearcher' import FileSearcher from '@/node/fileSearcher'
const MD_EXTENSION = /\.(?:markdown|mdown|mkdn|md|mkd|mdwn|mdtxt|mdtext|text|txt)$/i
const SPECIAL_CHARS = /[\[\]\\^$.\|\?\*\+\(\)\/]{1}/g // eslint-disable-line no-useless-escape const SPECIAL_CHARS = /[\[\]\\^$.\|\?\*\+\(\)\/]{1}/g // eslint-disable-line no-useless-escape
// The quick open command // The quick open command
@ -174,11 +173,11 @@ class QuickOpenCommand {
_getInclusions = query => { _getInclusions = query => {
// NOTE: This will fail on `foo.m` because we search for `foo.m.md`. // NOTE: This will fail on `foo.m` because we search for `foo.m.md`.
if (MD_EXTENSION.test(query)) { if (hasMarkdownExtension(query)) {
return [`*${query}`] return [`*${query}`]
} }
const inclusions = ['*.markdown', '*.mdown', '*.mkdn', '*.md', '*.mkd', '*.mdwn', '*.mdtxt', '*.mdtext', '*.text', '*.txt'] const inclusions = MARKDOWN_INCLUSIONS
for (let i = 0; i < inclusions.length; ++i) { for (let i = 0; i < inclusions.length; ++i) {
inclusions[i] = `*${query}` + inclusions[i] inclusions[i] = `*${query}` + inclusions[i]
} }

View File

@ -89,6 +89,7 @@ import EmptyIcon from '@/assets/icons/undraw_empty.svg'
import FindCaseIcon from '@/assets/icons/searchIcons/iconCase.svg' import FindCaseIcon from '@/assets/icons/searchIcons/iconCase.svg'
import FindWordIcon from '@/assets/icons/searchIcons/iconWord.svg' import FindWordIcon from '@/assets/icons/searchIcons/iconWord.svg'
import FindRegexIcon from '@/assets/icons/searchIcons/iconRegex.svg' import FindRegexIcon from '@/assets/icons/searchIcons/iconRegex.svg'
import { MARKDOWN_INCLUSIONS } from '../../../common/filesystem/paths'
export default { export default {
data () { data () {
@ -232,7 +233,7 @@ export default {
followSymlinks: this.searchFollowSymlinks, followSymlinks: this.searchFollowSymlinks,
// Only search markdown files // Only search markdown files
inclusions: ['*.markdown', '*.mdown', '*.mkdn', '*.md', '*.mkd', '*.mdwn', '*.mdtxt', '*.mdtext', '*.text', '*.txt'] inclusions: MARKDOWN_INCLUSIONS
}) })
.then(() => { .then(() => {
this.searchResult = newSearchResult this.searchResult = newSearchResult

View File

@ -6,6 +6,7 @@ import { create, paste, rename } from '../util/fileSystem'
import { PATH_SEPARATOR } from '../config' import { PATH_SEPARATOR } from '../config'
import notice from '../services/notification' import notice from '../services/notification'
import { getFileStateFromData } from './help' import { getFileStateFromData } from './help'
import { hasMarkdownExtension } from '../../common/filesystem/paths'
const state = { const state = {
activeItem: {}, activeItem: {},
@ -173,7 +174,13 @@ const actions = {
CREATE_FILE_DIRECTORY ({ commit, state }, name) { CREATE_FILE_DIRECTORY ({ commit, state }, name) {
const { dirname, type } = state.createCache const { dirname, type } = state.createCache
if (type === 'file' && !hasMarkdownExtension(name)) {
name += '.md'
}
const fullName = `${dirname}/${name}` const fullName = `${dirname}/${name}`
create(fullName, type) create(fullName, type)
.then(() => { .then(() => {
commit('CREATE_PATH', {}) commit('CREATE_PATH', {})