-
-
Notifications
You must be signed in to change notification settings - Fork 639
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract nitro logic from transformers (#1352)
Co-authored-by: Yaël Guilloux <[email protected]>
- Loading branch information
Showing
18 changed files
with
239 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
export { serverQueryContent } from './storage' | ||
export { parseContent } from './transformers' | ||
export { serverQueryContent, parseContent } from './storage' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ParsedContent } from '../types' | ||
import { defineTransformer } from './utils' | ||
|
||
export default defineTransformer({ | ||
name: 'csv', | ||
extensions: ['.csv'], | ||
parse: async (_id, content, options = {}) => { | ||
const csvToJson: any = await import('csvtojson').then(m => m.default || m) | ||
|
||
const parsed = await csvToJson({ output: 'json', ...options }) | ||
.fromString(content) | ||
|
||
return <ParsedContent> { | ||
_id, | ||
_type: 'csv', | ||
body: parsed | ||
} | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { extname } from 'pathe' | ||
import { camelCase } from 'scule' | ||
import type { ContentTransformer, TransformContentOptions } from '../types' | ||
import csv from './csv' | ||
import markdown from './markdown' | ||
import yaml from './yaml' | ||
import pathMeta from './path-meta' | ||
import json from './json' | ||
import highlight from './shiki' | ||
|
||
const TRANSFORMERS = [ | ||
csv, | ||
markdown, | ||
json, | ||
yaml, | ||
pathMeta, | ||
highlight | ||
] | ||
|
||
function getParser (ext, additionalTransformers: ContentTransformer[] = []): ContentTransformer { | ||
let parser = additionalTransformers.find(p => ext.match(new RegExp(p.extensions.join('|'), 'i')) && p.parse) | ||
if (!parser) { | ||
parser = TRANSFORMERS.find(p => ext.match(new RegExp(p.extensions.join('|'), 'i')) && p.parse) | ||
} | ||
|
||
return parser | ||
} | ||
|
||
function getTransformers (ext, additionalTransformers: ContentTransformer[] = []) { | ||
return [ | ||
...additionalTransformers.filter(p => ext.match(new RegExp(p.extensions.join('|'), 'i')) && p.transform), | ||
...TRANSFORMERS.filter(p => ext.match(new RegExp(p.extensions.join('|'), 'i')) && p.transform) | ||
] | ||
} | ||
|
||
/** | ||
* Parse content file using registered plugins | ||
*/ | ||
export async function transformContent (id, content, options: TransformContentOptions = {}) { | ||
const { transformers = [] } = options | ||
// Call hook before parsing the file | ||
const file = { _id: id, body: content } | ||
|
||
const ext = extname(id) | ||
const parser: ContentTransformer = getParser(ext, transformers) | ||
if (!parser) { | ||
// eslint-disable-next-line no-console | ||
console.warn(`${ext} files are not supported, "${id}" falling back to raw content`) | ||
return file | ||
} | ||
|
||
const parserOptions = options[camelCase(parser.name)] || {} | ||
const parsed = await parser.parse!(file._id, file.body, parserOptions) | ||
|
||
const matchedTransformers = getTransformers(ext, transformers) | ||
const result = await matchedTransformers.reduce(async (prev, cur) => { | ||
const next = (await prev) || parsed | ||
|
||
const transformOptions = options[camelCase(cur.name)] || {} | ||
return cur.transform!(next, transformOptions) | ||
}, Promise.resolve(parsed)) | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.