Skip to content

Commit

Permalink
Fix marktext#1879 Add .md extension to files created in sidebar if no…
Browse files Browse the repository at this point in the history
… extension is given (marktext#1886)

* Fix marktext#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.
  • Loading branch information
davisriedel authored Feb 8, 2020
1 parent 622ff90 commit 8623243
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/common/filesystem/paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export const MARKDOWN_EXTENSIONS = [
'txt'
]

export const MARKDOWN_INCLUSIONS = MARKDOWN_EXTENSIONS.map(x => '*.' + x)

export const IMAGE_EXTENSIONS = [
'jpeg',
'jpg',
Expand Down
7 changes: 3 additions & 4 deletions src/renderer/commands/quickOpen.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import path from 'path'
import { ipcRenderer } from 'electron'
import { isChildOfDirectory } from 'common/filesystem/paths'
import { isChildOfDirectory, hasMarkdownExtension, MARKDOWN_INCLUSIONS } from '../../common/filesystem/paths'
import bus from '../bus'
import { delay } from '@/util'
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

// The quick open command
Expand Down Expand Up @@ -174,11 +173,11 @@ class QuickOpenCommand {

_getInclusions = query => {
// NOTE: This will fail on `foo.m` because we search for `foo.m.md`.
if (MD_EXTENSION.test(query)) {
if (hasMarkdownExtension(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) {
inclusions[i] = `*${query}` + inclusions[i]
}
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/components/sideBar/search.vue
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import EmptyIcon from '@/assets/icons/undraw_empty.svg'
import FindCaseIcon from '@/assets/icons/searchIcons/iconCase.svg'
import FindWordIcon from '@/assets/icons/searchIcons/iconWord.svg'
import FindRegexIcon from '@/assets/icons/searchIcons/iconRegex.svg'
import { MARKDOWN_INCLUSIONS } from '../../../common/filesystem/paths'
export default {
data () {
Expand Down Expand Up @@ -232,7 +233,7 @@ export default {
followSymlinks: this.searchFollowSymlinks,
// Only search markdown files
inclusions: ['*.markdown', '*.mdown', '*.mkdn', '*.md', '*.mkd', '*.mdwn', '*.mdtxt', '*.mdtext', '*.text', '*.txt']
inclusions: MARKDOWN_INCLUSIONS
})
.then(() => {
this.searchResult = newSearchResult
Expand Down
7 changes: 7 additions & 0 deletions src/renderer/store/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { create, paste, rename } from '../util/fileSystem'
import { PATH_SEPARATOR } from '../config'
import notice from '../services/notification'
import { getFileStateFromData } from './help'
import { hasMarkdownExtension } from '../../common/filesystem/paths'

const state = {
activeItem: {},
Expand Down Expand Up @@ -173,7 +174,13 @@ const actions = {

CREATE_FILE_DIRECTORY ({ commit, state }, name) {
const { dirname, type } = state.createCache

if (type === 'file' && !hasMarkdownExtension(name)) {
name += '.md'
}

const fullName = `${dirname}/${name}`

create(fullName, type)
.then(() => {
commit('CREATE_PATH', {})
Expand Down

0 comments on commit 8623243

Please sign in to comment.