Skip to content

Commit

Permalink
feat: plugin feature
Browse files Browse the repository at this point in the history
  • Loading branch information
zce committed Nov 12, 2023
1 parent 8a2e2be commit f89b017
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 35 deletions.
4 changes: 4 additions & 0 deletions .github/renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": ["github>zce/renovate-config"]
}
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
name: CI

on: push

jobs:
publish:
if: startsWith(github.ref, 'refs/tags')
Expand Down
10 changes: 0 additions & 10 deletions .vscode/extensions.json

This file was deleted.

12 changes: 2 additions & 10 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.quickSuggestions": {
"strings": true
},
"explorer.fileNesting.enabled": true,
"explorer.fileNesting.expand": false,
"explorer.fileNesting.patterns": {
"package.json": "package-lock.json, yarn.lock, pnpm-lock.yaml",
"readme.md": "license, changelog.md"
"readme.md": "license"
},
// exclude files that are not frequently modified or built.
"files.exclude": {
"node_modules": true
},
"javascript.preferences.quoteStyle": "single",
"javascript.updateImportsOnFileMove.enabled": "never",
"typescript.preferences.quoteStyle": "single",
"typescript.tsdk": "node_modules/typescript/lib",
"typescript.updateImportsOnFileMove.enabled": "never"
"typescript.tsdk": "node_modules/typescript/lib"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"dist"
],
"scripts": {
"build": "tsup",
"build": "tsc && tsup",
"format": "prettier --write ."
},
"tsup": {
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
[![Dependency Status][dependency-img]][dependency-url]
[![Code Style][style-img]][style-url]

## TODOs

- [ ] nextjs plugin

## Installation

```shell
Expand Down
26 changes: 15 additions & 11 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ import { bundleRequire } from 'bundle-require'
import JoyCon from 'joycon'

import { name } from '../package.json'
import { addLoader } from './loader'
import { addLoader, addPlugin } from './loader'
import { init } from './static'

import type { Loader } from './loader'
import type { Loader, Plugin } from './loader'
import type { Collections } from './types'
import type { ZodType } from 'zod'

type Schema = {
name: string
pattern: string
single?: boolean
fields: ZodType
Expand All @@ -29,6 +28,7 @@ type Config = {
}
schemas: { [name: string]: Schema }
loaders?: Loader[]
plugins?: Plugin[]
callback: (collections: Collections) => void | Promise<void>
}

Expand All @@ -40,17 +40,17 @@ type Options = {

const joycon = new JoyCon()

joycon.addLoader({
test: /\.(js|cjs|mjs|ts)$/,
load: async filepath => {
const { mod: config } = await bundleRequire({ filepath })
return config.default || config
}
})

export const resolveConfig = async (options: Options = {}): Promise<Config> => {
const configPaths = [options.filename, name + '.config.js', name + '.config.ts'].filter(Boolean) as string[]

joycon.addLoader({
test: /\.(js|cjs|mjs|ts)$/,
load: async filepath => {
const { mod: config } = await bundleRequire({ filepath })
return config.default || config
}
})

const { data: config, path } = await joycon.load(configPaths)

options.verbose && console.log(`using config '${path}'`)
Expand All @@ -61,6 +61,10 @@ export const resolveConfig = async (options: Options = {}): Promise<Config> => {
config.loaders.forEach((loader: Loader) => addLoader(loader))
}

if (config.plugins != null) {
config.plugins.forEach((plugin: Plugin) => addPlugin(plugin))
}

init(config.output.static, config.output.base)

return config
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { z, shared } from './shared'
export { defineConfig } from './config'
export { addLoader, removeLoader } from './loader'
export { addLoader, removeLoader, addPlugin, removePlugin } from './loader'
export { build } from './build'
export { image } from './image'
31 changes: 31 additions & 0 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,24 @@ export type Loader = {
load: (file: VFile) => Promise<unknown>
}

type MdastPlugin = {
name: string
type: 'mdast'
apply: (mdast: ReturnType<typeof fromMarkdown>) => void | Promise<void>
}

type HastPlugin = {
name: string
type: 'hast'
apply: (hast: ReturnType<typeof toHast>) => void | Promise<void>
}

export type Plugin = MdastPlugin | HastPlugin

const loaders: Loader[] = []

const plugins: Plugin[] = []

export const addLoader = (loader: Loader) => {
loaders.unshift(loader)
}
Expand All @@ -28,6 +44,14 @@ export const removeLoader = (name: string) => {
const loader = loaders.find(loader => loader.name === name)
loader && loaders.splice(loaders.indexOf(loader), 1)
}
export const addPlugin = (plugin: Plugin) => {
plugins.unshift(plugin)
}

export const removePlugin = (name: string) => {
const plugin = plugins.find(plugin => plugin.name === name)
plugin && plugins.splice(plugins.indexOf(plugin), 1)
}

export const load = (file: VFile) => {
const loader = loaders.find(loader => loader.test.test(file.path))
Expand Down Expand Up @@ -118,10 +142,17 @@ addLoader({
)
// #endregion

// apply mdast plugins
await Promise.all(plugins.map(async p => p.type === 'mdast' && (await p.apply(mdast))))

// generate markdown
data.raw = toMarkdown(mdast, { extensions: [gfmToMarkdown()] })
// parse to hast
const hast = raw(toHast(mdast, { allowDangerousHtml: true }))

// apply hast plugins
await Promise.all(plugins.map(async p => p.type === 'hast' && (await p.apply(hast))))

// console.log((await import('unist-util-inspect')).inspect(hast))
const lines: string[] = []
visit(hast, 'text', node => {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"module": "es2022",
"moduleResolution": "bundler",
"esModuleInterop": true,
"noEmit": true,
"strict": true
}
}
1 change: 1 addition & 0 deletions types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'load-tsconfig'

0 comments on commit f89b017

Please sign in to comment.