qibogang / qibogang.github.io

The gang site :)
https://qibogang.github.io
0 stars 0 forks source link

Better control of the files' extensions into the `GetStatic` functions. #18

Open MatteoRobbiati opened 1 year ago

MatteoRobbiati commented 1 year ago

It should be better to have a customized procedure for reading extension for each file, in order to avoid to use strings like .mdx or .md in that process.

A smart way for extracting the extension is to use path.extname provided by the already involved package path, acting for example in the following way:

const files = fs.readdirSync(path.join('tutorials'))
var files_extensions = []

for(let i=0; i<files.length; i++){
      files_extensions.push(path.extname(files[i]))
}
alecandido commented 1 year ago

The goal is to check whether the extension of the file is in a list of allowed extensions.

For Markdown notes, the list should be [".md", ".mdx"]. Another option is to check against a RegExp pattern, e.g. /\.mdx?/. (or matches at least one pattern in a list, but most likely this is more than what we need)

alecandido commented 1 year ago

path is part of Node.js standard library: https://nodejs.org/api/path.html

MatteoRobbiati commented 1 year ago

The goal is to check whether the extension of the file is in a list of allowed extensions.

This is true for avoiding unsupported files in the tutorials. The extname function also solves the problem of unlucky names like you said in a previous comment.