mirror of
https://git.hmsn.ink/kospo/svcm/oa.git
synced 2026-03-20 08:13:40 +09:00
74 lines
2.4 KiB
TypeScript
74 lines
2.4 KiB
TypeScript
import { kebabCase } from 'scule'
|
|
|
|
const SELF_CLOSING_TAG_REGEX = /<([^\s></]+)([^>]+)\/>/g
|
|
const OPEN_TAG_REGEX = /<([^\s></]+)/g
|
|
const CLOSE_TAG_REGEX = /<\/([^\s></]+)/g
|
|
|
|
export function transformExampleMarkup(raw: string) {
|
|
let output = raw
|
|
let content: RegExpMatchArray | null = null
|
|
if ((content = raw.match(/<!--example-->([\s\S]*?)<!--\/example-->/))) {
|
|
const kebabContent = content[1]
|
|
.replaceAll(SELF_CLOSING_TAG_REGEX, (substring, tag) => {
|
|
return substring.replace('/>', `></${tag.trim()}>`)
|
|
})
|
|
.replaceAll(OPEN_TAG_REGEX, (substring) => {
|
|
return `<${kebabCase(substring.substring(1).trim())}`
|
|
})
|
|
.replaceAll(CLOSE_TAG_REGEX, (substring) => {
|
|
return `</${kebabCase(substring.substring(2).trim())}`
|
|
})
|
|
.replaceAll(''', '\'')
|
|
|
|
output = output.replace(content[1], kebabContent)
|
|
}
|
|
|
|
return output
|
|
}
|
|
|
|
export function transformSlots(source: string, condition: string = '') {
|
|
if (source.includes('<!--code-->') && source.includes('<!--example-->')) {
|
|
return `<template ${condition} #default>${source}`
|
|
.replace(
|
|
`<!--code-->`,
|
|
`</template><template ${condition} #code>\n<slot name="code"><div v-pre>`,
|
|
)
|
|
.replace(`<!--/code-->`, `</div></slot>\n</template>`)
|
|
.replace(
|
|
`<!--example-->`,
|
|
`<template ${condition} #example>\n<slot name="example">`,
|
|
)
|
|
.replace(`<!--/example-->`, `</slot>\n</template>`)
|
|
}
|
|
|
|
if (source.includes('<!--code-->')) {
|
|
return `<template ${condition} #default>${source}`
|
|
.replace(
|
|
`<!--code-->`,
|
|
`</template><template ${condition} #code>\n<slot name="code"><div v-pre>`,
|
|
)
|
|
.replace(
|
|
`<!--/code-->`,
|
|
`</div></slot>\n</template>\n<template ${condition} #example><slot name="example"></slot></template>`,
|
|
)
|
|
}
|
|
|
|
if (source.includes('<!--example-->')) {
|
|
return `<template ${condition} #default>${source}`
|
|
.replace(
|
|
`<!--example-->`,
|
|
`</template><template ${condition} #example>\n<slot name="example">`,
|
|
)
|
|
.replace(
|
|
`<!--/example-->`,
|
|
`</slot>\n</template>\n<template ${condition} #code><slot name="code"></slot></template>`,
|
|
)
|
|
}
|
|
|
|
return (
|
|
`<template ${condition} #default>${source}</template>`
|
|
+ `<template ${condition} #example><slot name="example"></slot></template>`
|
|
+ `<template ${condition} #code><slot name="code"></slot></template>`
|
|
)
|
|
}
|