Skip to content

Commit

Permalink
feat(module): integrate with unifiedjs VFile (#2911)
Browse files Browse the repository at this point in the history
  • Loading branch information
farnabaz authored Dec 12, 2024
1 parent 7a6056a commit 6371a19
Show file tree
Hide file tree
Showing 20 changed files with 207 additions and 59 deletions.
4 changes: 4 additions & 0 deletions playground/content/remark-code-import.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Below is the content of `0.index.md`

```vue [0.index.md] file=./0.index.md
```
3 changes: 3 additions & 0 deletions playground/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export default defineNuxtConfig({
content: {
build: {
markdown: {
remarkPlugins: {
'remark-code-import': {},
},
highlight: {
theme: {
dark: 'aurora-x', // Theme containing italic
Expand Down
1 change: 1 addition & 0 deletions playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"dev:prepare": "nuxt prepare"
},
"dependencies": {
"remark-code-import": "latest",
"@nuxt/content": "latest",
"@nuxt/ui-pro": "3.0.0-alpha.9",
"@nuxthub/core": "^0.8.6",
Expand Down
91 changes: 91 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 10 additions & 3 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type { ResolvedCollection } from './types/collection'
import type { ModuleOptions, SqliteDatabaseConfig } from './types/module'
import { getContentChecksum, localDatabase, logger, watchContents, chunks, watchComponents, watchConfig, startSocketServer } from './utils/dev'
import { loadLayersConfig } from './utils/config'
import { parseContent } from './utils/content'
import { createParser } from './utils/content'
import { installMDCModule } from './utils/mdc'
import { findPreset } from './presets'
import type { Manifest } from './types/manifest'
Expand Down Expand Up @@ -268,6 +268,8 @@ async function processCollectionItems(nuxt: Nuxt, collections: ResolvedCollectio
if (!collection.source) {
continue
}
const parse = await createParser(collection, nuxt)

for await (const source of collection.source) {
if (source.prepare) {
await source.prepare(nuxt)
Expand All @@ -285,8 +287,9 @@ async function processCollectionItems(nuxt: Nuxt, collections: ResolvedCollectio
await Promise.all(chunk.map(async (key) => {
key = key.substring(fixed.length)
const keyInCollection = join(collection.name, source?.prefix || '', key)
const fullPath = join(cwd, fixed, key)

const content = await readFile(join(cwd, fixed, key), 'utf8')
const content = await readFile(fullPath, 'utf8')
const checksum = getContentChecksum(configHash + collectionHash + content)
const cache = databaseContents[keyInCollection]

Expand All @@ -297,7 +300,11 @@ async function processCollectionItems(nuxt: Nuxt, collections: ResolvedCollectio
}
else {
parsedFilesCount += 1
parsedContent = await parseContent(keyInCollection, content, collection, nuxt)
parsedContent = await parse({
id: keyInCollection,
body: content,
path: fullPath,
})
db.insertDevelopmentCache(keyInCollection, checksum, JSON.stringify(parsedContent))
}

Expand Down
12 changes: 10 additions & 2 deletions src/types/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ import type { Highlighter, MDCRoot, Toc } from '@nuxtjs/mdc'

export type { Toc, TocLink } from '@nuxtjs/mdc'

export interface ContentFile extends Record<string, unknown> {
id: string
body: string
path: string
dirname?: string
extension?: string
}

export interface TransformedContent {
id: string
[key: string]: unknown
Expand All @@ -15,12 +23,12 @@ export interface TransformContentOptions {
export type ContentTransformer = {
name: string
extensions: string[]
parse(id: string, content: string, options: Record<string, unknown>): Promise<TransformedContent> | TransformedContent
parse(file: ContentFile, options: Record<string, unknown>): Promise<TransformedContent> | TransformedContent
transform?(content: TransformedContent, options: Record<string, unknown>): Promise<TransformedContent> | TransformedContent
} | {
name: string
extensions: string[]
parse?(id: string, content: string, options: Record<string, unknown>): Promise<TransformedContent> | TransformedContent
parse?(file: ContentFile, options: Record<string, unknown>): Promise<TransformedContent> | TransformedContent
transform(content: TransformedContent, options: Record<string, unknown>): Promise<TransformedContent> | TransformedContent
}

Expand Down
Loading

0 comments on commit 6371a19

Please sign in to comment.