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

fix(core): ignore invalid extensions #423

Merged
merged 1 commit into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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)
Copy link
Contributor

@pi0 pi0 Jun 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Advice: You shouldn't implement this on storage layer! This is an abstracted driver that should be replaceable. The consumer should do filtering.... (otherwise there is no point of using unstorage)


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