mirror of
https://github.com/fmhy/edit.git
synced 2025-07-30 07:42:18 +10:00
feat: custom transformer composable
This commit is contained in:
parent
5636b5487a
commit
422ffec7e8
5 changed files with 295 additions and 210 deletions
53
docs/.vitepress/transformer/core.ts
Normal file
53
docs/.vitepress/transformer/core.ts
Normal file
|
@ -0,0 +1,53 @@
|
|||
import consola from 'consola'
|
||||
|
||||
type Transform = {
|
||||
name: string
|
||||
find: string | RegExp
|
||||
replace: string | ((match: string) => string)
|
||||
}
|
||||
|
||||
type TransformerFunc = (name: string, transforms: Transform[]) => Replacer
|
||||
|
||||
interface Replacer {
|
||||
transform: TransformerFunc
|
||||
getText(): string
|
||||
}
|
||||
|
||||
export const transformer = (text: string) => {
|
||||
const handler: ProxyHandler<{ text: string }> = {
|
||||
get(target, prop) {
|
||||
if (prop === 'transform') {
|
||||
return (name: string, transforms: Transform[]): Replacer => {
|
||||
consola.debug(`Starting transform ${name} with ${transforms}`)
|
||||
|
||||
transforms.forEach(({ name, find, replace }) => {
|
||||
consola.debug(`Transforming ${name} with ${find}`)
|
||||
target.text = target.text.replace(find, replace as any)
|
||||
})
|
||||
|
||||
// @ts-expect-error - Proxy is not typed
|
||||
return proxy
|
||||
}
|
||||
}
|
||||
if (prop === 'getText') {
|
||||
return () => target.text
|
||||
}
|
||||
return Reflect.get(target, prop)
|
||||
}
|
||||
}
|
||||
|
||||
const target = { text }
|
||||
const proxy = new Proxy(target, handler)
|
||||
return proxy as unknown as Replacer
|
||||
}
|
||||
|
||||
export function replaceUnderscore(text: string): string {
|
||||
const pattern = /\/#[\w\-]+(?:_[\w]+)*/g
|
||||
const matches = text.match(pattern) || []
|
||||
let _text = text
|
||||
for (const match of matches) {
|
||||
const replacement = match.replace(/_/g, '-')
|
||||
_text = _text.replace(match, replacement)
|
||||
}
|
||||
return _text
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue