mirror of
https://github.com/Snigdha-OS/documentation.git
synced 2025-09-09 19:44:56 +02:00
54 lines
1.3 KiB
JavaScript
54 lines
1.3 KiB
JavaScript
/**
|
|
* @typedef {import('vfile').Compatible} Compatible
|
|
* @typedef {import('../compile.js').CompileOptions} CompileOptions
|
|
* @typedef {import('../core.js').ProcessorOptions} ProcessorOptions
|
|
*/
|
|
|
|
import {VFile} from 'vfile'
|
|
import {md} from './extnames.js'
|
|
|
|
/**
|
|
* Create a file and options from a given `vfileCompatible` and options that
|
|
* might contain `format: 'detect'`.
|
|
*
|
|
* @param {Readonly<Compatible>} vfileCompatible
|
|
* File.
|
|
* @param {Readonly<CompileOptions> | null | undefined} [options]
|
|
* Configuration (optional).
|
|
* @returns {{file: VFile, options: ProcessorOptions}}
|
|
* File and options.
|
|
*/
|
|
export function resolveFileAndOptions(vfileCompatible, options) {
|
|
const file = looksLikeAVFile(vfileCompatible)
|
|
? vfileCompatible
|
|
: new VFile(vfileCompatible)
|
|
const {format, ...rest} = options || {}
|
|
return {
|
|
file,
|
|
options: {
|
|
format:
|
|
format === 'md' || format === 'mdx'
|
|
? format
|
|
: file.extname && (rest.mdExtensions || md).includes(file.extname)
|
|
? 'md'
|
|
: 'mdx',
|
|
...rest
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @param {Readonly<Compatible> | null | undefined} [value]
|
|
* Thing.
|
|
* @returns {value is VFile}
|
|
* Check.
|
|
*/
|
|
function looksLikeAVFile(value) {
|
|
return Boolean(
|
|
value &&
|
|
typeof value === 'object' &&
|
|
'message' in value &&
|
|
'messages' in value
|
|
)
|
|
}
|