export interface TabsData { id: string } /** * Decode the entities emitted by `stringifyProp` in a single pass, the same way * the HTML parser decodes an attribute before Vue compiles the expression. * * `&` is decoded last, so a decoded `&` is not re-interpreted as the start * of another entity. * * @param raw - The raw attribute value / 原始属性值 * @returns The decoded attribute value / 解码后的属性值 */ export const decodePropAttribute = (raw: string): string => raw .replaceAll('"', '"') .replaceAll(''', "'") .replaceAll('&', '&') /** * Extract and parse the `:data` attribute of a tabs component. * * @param output - The rendered markdown / 渲染后的 Markdown * @param tag - The tabs component tag / 选项卡组件标签 * @returns The raw attribute value and the parsed data / 原始属性值与解析后的数据 */ export const extractTabsData = ( output: string, tag: 'VPCodeTabs' | 'VPTabs', ): { raw: string; data: TabsData[] } => { const match = new RegExp(`<${tag} :data='([^']*)'`, 'u').exec(output) if (!match) throw new Error(`Missing ${tag} :data attribute`) const [, raw] = match return { raw, data: JSON.parse(decodePropAttribute(raw)) as TabsData[] } }