Skip to content

Commit

Permalink
fix(core): ignore invalid extensions (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz authored Jun 14, 2021
1 parent a94b38c commit 2fdc18f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/core/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,9 @@ export default <Module>async function docusModule() {
drivers: [
{
base: resolve(options.srcDir, $docus.settings.contentDir),
// mount point of driver
mountPoint: 'pages',
// List of Nuxt ignore rules
ignore: await useNuxtIgnoreList(nuxt)
},
{
Expand Down
8 changes: 8 additions & 0 deletions src/core/parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ export function initParser(options: Partial<ParserOptions> = {}) {

export function useParser() {
return {
/**
* The list of all extensions that have specific parser in Docus
* @returns list of valid extensions
*/
extensions() {
return Object.keys(parsers)
},

parse: async (file, content) => {
const extension = extname(file)
const path = '/' + file.replace(new RegExp(`${extension}$`), '')
Expand Down
25 changes: 23 additions & 2 deletions src/core/storage/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,20 @@ export const docusDriver = defineDriver((options: DriverOptions) => {

const { insert, items } = useDB()
const { callHook } = useHooks()
const parser = useParser()
const fs = fsDriver(options)

const parser = useParser()

// validate key based on its extension
const isValidKey = (key: string) => parser.extensions().some(ext => key.endsWith(ext))

/**
* parse specific document and insert parsed data into database
*
* @param key document file key
* @param content documnet conent
* @returns parsed object
*/
const parseAndInsert = async (key, content) => {
const document = await parser.parse(key, content)

Expand Down Expand Up @@ -115,7 +126,14 @@ export const docusDriver = defineDriver((options: DriverOptions) => {
}

// retrive contents list
const getKeys = () => fs.getKeys()
const getKeys = async () => {
let keys = await fs.getKeys()

// filter valid keys
keys = keys.filter(isValidKey)

return keys
}

const hasItem = key => fs.hasItem(key)

Expand Down Expand Up @@ -175,6 +193,9 @@ export const docusDriver = defineDriver((options: DriverOptions) => {
// Watch files and revalidate data
const watch = callback => {
return fs.watch(async (event, key) => {
// ignore invalid extensions
if (!isValidKey(key)) return

if (event === 'update') {
const content = await fs.getItem(key)

Expand Down

0 comments on commit 2fdc18f

Please sign in to comment.