Skip to content

Commit

Permalink
feat: Pipeline processing adapted to leaner transform function and ca…
Browse files Browse the repository at this point in the history
…rries BuilderApi generic state throughout
  • Loading branch information
yankeeinlondon committed Dec 16, 2022
1 parent d44f7b5 commit ecad953
Show file tree
Hide file tree
Showing 36 changed files with 715 additions and 569 deletions.
11 changes: 10 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
"extends": "@antfu",
"rules": {
"jsonc/sort-keys": "off",
"eol-last": "off"
"eol-last": "off",
// "cases" allows for graceful use of that variable
// in type testing
"@typescript-eslint/no-unused-vars": [
"error",
{
"varsIgnorePattern": "cases|^_",
"argsIgnorePattern": "^_"
}
]
}
}
16 changes: 8 additions & 8 deletions example/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import Components from 'unplugin-vue-components/vite'
import Inspect from 'vite-plugin-inspect'
import Layouts from 'vite-plugin-vue-layouts'
import Markdown from 'vite-plugin-md'
import code from '@yankeeinlondon/code-builder'
import link from '@yankeeinlondon/link-builder'
import meta from '@yankeeinlondon/meta-builder'
// import code from '@yankeeinlondon/code-builder'
// import link from '@yankeeinlondon/link-builder'
// import meta from '@yankeeinlondon/meta-builder'
import Pages from 'vite-plugin-pages'
import Unocss from 'unocss/vite'
import Vue from '@vitejs/plugin-vue'
Expand Down Expand Up @@ -60,11 +60,11 @@ const config = defineConfig({
baseStyle: 'github',
},
builders: [
meta(),
link(),
code({
theme: 'base',
}),
// meta(),
// link(),
// code({
// theme: 'base',
// }),
],
}),

Expand Down
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@
"test:component": "npx cypress open-ct -p 4000"
},
"dependencies": {
"@yankeeinlondon/builder-api": "^1.2.2",
"@yankeeinlondon/builder-api": "^1.3.4",
"@yankeeinlondon/gray-matter": "^6.1.1",
"@yankeeinlondon/happy-wrapper": "^2.10.1",
"markdown-it": "^13.0.1",
"source-map-js": "^1.0.2"
},
"peerDependencies": {
"vite": "^4.0.0 || ^3.0.0",
"@vitejs/plugin-vue": ">=2.3.4"
"@vitejs/plugin-vue": ">=2.3.4",
"vite": "^4.0.0 || ^3.0.0"
},
"devDependencies": {
"@antfu/eslint-config": "^0.34.0",
Expand All @@ -78,7 +78,7 @@
"@vitest/coverage-c8": "^0.25.8",
"@vitest/ui": "^0.25.8",
"@vue/test-utils": "^2.2.6",
"@vueuse/core": "^9.6.0",
"@vueuse/core": "^9.7.0",
"@yankeeinlondon/code-builder": "^1.2.1",
"@yankeeinlondon/link-builder": "^1.2.1",
"@yankeeinlondon/meta-builder": "^1.2.1",
Expand All @@ -89,6 +89,7 @@
"eslint-config-prettier": "^8.5.0",
"fp-ts": "^2.13.1",
"happy-dom": "^8.1.0",
"native-dash": "^1.25.0",
"npm-run-all": "^4.1.5",
"pathe": "^1.0.0",
"rollup": "^3.7.4",
Expand All @@ -99,4 +100,4 @@
"vue": "^3.2.45",
"vue-router": "^4.1.6"
}
}
}
51 changes: 39 additions & 12 deletions pnpm-lock.yaml

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

78 changes: 37 additions & 41 deletions src/composeSfcBlocks.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { flow, pipe } from 'fp-ts/lib/function.js'
import { isRight } from 'fp-ts/lib/Either.js'

import { resolveOptions } from './options'
import { PipelineStage } from './types'
import type {
GenericBuilder,
Options,
Pipeline,
ViteConfigPassthrough,
Expand All @@ -18,13 +19,11 @@ import {
finalize,
frontmatterPreprocess,
gatherBuilderEvents,
injectUtilityFunctions,
loadMarkdownItPlugins,
parseHtml,
repairFrontmatterLinks,
sourcemap,
transformsBefore,
usesBuilder,
wrapHtml,
} from './pipeline'
import { lift } from './utils'
Expand All @@ -35,14 +34,21 @@ import { kebabCaseComponents } from './pipeline/kebabCaseComponents'
* Composes the `template` and `script` blocks, along with any other `customBlocks` from
* the raw markdown content along with user options.
*/
export async function composeSfcBlocks(
export async function composeSfcBlocks<
B extends readonly GenericBuilder[] = [],
>(
id: string,
raw: string,
opts: Omit<Options, 'usingBuilder'> = {},
opts: Options<B> = {} as Options<B>,
config: Partial<ViteConfigPassthrough> = {},
) {
const options = resolveOptions(opts)
const p0 = {

/**
* The initial pipeline state
*/
const payload: Pipeline<'initialize', B> = {
stage: 'initialize',
fileName: id,
content: raw.trimStart(),
head: {},
Expand All @@ -58,78 +64,68 @@ export async function composeSfcBlocks(
options,
}

/**
* The initial pipeline state
*/
const payload: Pipeline<PipelineStage.initialize> = {
...p0,
usesBuilder: usesBuilder(p0 as unknown as Pipeline<PipelineStage.initialize>, []),
}

const handlers = gatherBuilderEvents(options)

/** initialize the configuration */
const initialize = flow(
lift('initialize'),
transformsBefore,
handlers(PipelineStage.initialize),
// addBuilderDependencies([]),
// lifted,
transformsBefore<B>(),
handlers('initialize'),
)

/** extract the meta-data from the MD content */
const metaExtracted = flow(
injectUtilityFunctions,
extractFrontmatter,
baseStyling,
frontmatterPreprocess,
handlers(PipelineStage.metaExtracted),
extractFrontmatter<B>(),
baseStyling<B>(),
frontmatterPreprocess<B>(),
handlers('metaExtracted'),
)

/** establish the MarkdownIt parser */
const parser = flow(
createParser,
loadMarkdownItPlugins,
applyMarkdownItOptions,
handlers(PipelineStage.parser),
createParser<B>(),
loadMarkdownItPlugins<B>(),
applyMarkdownItOptions<B>(),
handlers('parser'),
)

/**
* use MarkdownIt to produce HTML
*/
const parsed = flow(
parseHtml,
kebabCaseComponents,
repairFrontmatterLinks,
wrapHtml,
handlers(PipelineStage.parsed),
parseHtml<B>(),
kebabCaseComponents<B>(),
repairFrontmatterLinks<B>(),
wrapHtml<B>(),
handlers('parsed'),
)

/**
* Convert HTML to DOM structure to make certain mutations
* easier to perform.
*/
const dom = flow(
convertToDom,
escapeCodeTagInterpolation,
handlers(PipelineStage.dom),
convertToDom<B>(),
escapeCodeTagInterpolation<B>(),
handlers('dom'),
)

// construct the async pipeline
const result = await pipe(
payload,
lift(payload),

initialize,
metaExtracted,
parser,
parsed,

dom,
extractBlocks,
handlers(PipelineStage.sfcBlocksExtracted),
extractBlocks<B>(),
handlers('sfcBlocksExtracted'),

finalize,
sourcemap,
handlers(PipelineStage.closeout),
finalize<B>(),
sourcemap<B>(),
handlers('closeout'),
)()

if (isRight(result))
Expand Down
Loading

0 comments on commit ecad953

Please sign in to comment.