{"version":3,"sources":["../src/index.mts","../src/rehype-add-referrerpolicy/index.mts","../src/rehype-remove-classnames/index.mts","../src/rehype-remove-styles/index.mts","../src/rehype-remove-yuque-wrapper/index.mts","../src/rehype-rewrite-links/index.mts"],"sourcesContent":["import { createRequire } from 'node:module';\n\nimport { mdxLoader } from '@secretflow/dumi-mdx-loader-core';\nimport { rehypeArticleOutline } from '@secretflow/unified-toolkit/rehype-article-outline';\nimport { rehypeBetterTable } from '@secretflow/unified-toolkit/rehype-better-table';\nimport { rehypeDumiMetadataModule } from '@secretflow/unified-toolkit/rehype-dumi-metadata-module';\nimport { rehypePrettyCode } from '@secretflow/unified-toolkit/rehype-pretty-code';\nimport { rehypeRemoveEmptyElements } from '@secretflow/unified-toolkit/rehype-remove-empty-elements';\nimport { remarkDumiContentModule } from '@secretflow/unified-toolkit/remark-dumi-content-module';\nimport { MDX_NODE_TYPES } from '@secretflow/unified-toolkit/utils';\nimport type { IApi as DumiAPI } from 'dumi';\nimport rehypeRaw from 'rehype-raw';\nimport remarkExtractFrontmatter from 'remark-extract-frontmatter';\nimport remarkFrontmatter from 'remark-frontmatter';\nimport YAML from 'yaml';\n\nimport { rehypeAddReferrerpolicy } from './rehype-add-referrerpolicy/index.mjs';\nimport { rehypeRemoveClassNames } from './rehype-remove-classnames/index.mjs';\nimport { rehypeRemoveStyle } from './rehype-remove-styles/index.mjs';\nimport { rehypeRemoveDumiWrapper } from './rehype-remove-yuque-wrapper/index.mjs';\nimport type { RouteMappingOptions } from './rehype-rewrite-links/index.mjs';\nimport { rehypeRewriteLinks } from './rehype-rewrite-links/index.mjs';\n\nconst require = createRequire(import.meta.url);\n\nexport function plugin(api: DumiAPI) {\n  mdxLoader(api)(() => ({\n    format: 'md',\n    extensions: ['html'],\n    pipelines: {\n      content: {\n        remarkPlugins: [\n          [remarkFrontmatter],\n          [remarkDumiContentModule, { builtins: api.service.themeData.builtins }],\n        ],\n        rehypePlugins: [\n          [rehypeRaw, { passThrough: MDX_NODE_TYPES }],\n          [rehypePrettyCode],\n          [rehypeRemoveClassNames],\n          [rehypeBetterTable],\n          [rehypeAddReferrerpolicy],\n          [\n            rehypeRemoveStyle,\n            {\n              properties: ['fontSize', 'lineHeight', { tr: 'height', td: 'width' }],\n            },\n          ],\n          [\n            rehypeRewriteLinks,\n            {\n              routes: api.appData['routes'],\n              match: /admin(\\/[0-9A-Za-z]+)$/,\n              resolve: (id, path) => path.endsWith(id),\n            } satisfies RouteMappingOptions,\n          ],\n          [rehypeRemoveEmptyElements],\n          [rehypeRemoveDumiWrapper],\n        ],\n      },\n      metadata: {\n        remarkPlugins: [\n          [remarkFrontmatter],\n          [remarkExtractFrontmatter, { yaml: YAML.parse, name: 'frontmatter' }],\n        ],\n        rehypePlugins: [\n          [rehypeRaw, { passThrough: MDX_NODE_TYPES }],\n          [rehypeArticleOutline],\n          [\n            rehypeDumiMetadataModule,\n            { legacySearch: true, routes: api.appData['routes'] },\n          ],\n        ],\n      },\n    },\n    preprocessor: (config) =>\n      config\n        .use('html-preprocessor')\n        .loader(require.resolve('./html-preprocessor/index.cjs'))\n        .end(),\n  }));\n}\n","import type { Element } from 'hast';\nimport type { Transformer } from 'unified';\nimport { visit } from 'unist-util-visit';\n\nexport function rehypeAddReferrerpolicy(): Transformer {\n  return (tree) => {\n    visit(tree, 'element', (node: Element) => {\n      {\n        /* 添加 referrerpolicy=no-referrer\n        解决访问语雀图片 referrer acl 限制问题 */\n      }\n      if (node.tagName === 'img') {\n        if (!node.properties) {\n          node.properties = {};\n          node.properties['referrerPolicy'] = 'no-referrer';\n        } else {\n          if (!node.properties['referrerPolicy']) {\n            node.properties['referrerPolicy'] = 'no-referrer';\n          }\n        }\n      }\n    });\n  };\n}\n","import type { Element } from 'hast';\nimport type { Transformer } from 'unified';\nimport { visit } from 'unist-util-visit';\n\nexport function rehypeRemoveClassNames(): Transformer {\n  return (tree) => {\n    visit(tree, 'element', (node: Element) => {\n      delete node.properties?.['className'];\n    });\n  };\n}\n","import type { Element } from 'hast';\nimport postcss from 'postcss';\nimport postcssJs from 'postcss-js';\nimport type { Transformer } from 'unified';\nimport { visit } from 'unist-util-visit';\n\ninterface Options {\n  properties: (string | Record<string, string | string[]>)[];\n}\n\nexport function rehypeRemoveStyle(options: Options): Transformer {\n  const { properties } = options;\n  return async (tree) => {\n    for (const property of properties) {\n      const nodes: Element[] = [];\n\n      // get all nodes by visit(): to solve the async problem,\n      // because the third argument cannot be async function\n      visit(tree, 'element', (node: Element) => {\n        nodes.push(node);\n      });\n\n      for (const node of nodes) {\n        let _property: string | string[] = '';\n        if (typeof property === 'string') {\n          _property = property;\n\n          await updateNodeStyle(node, _property);\n        } else if (property instanceof Object) {\n          _property = property[node.tagName];\n\n          if (_property instanceof Array) {\n            for (const _eachProperty of _property) {\n              await updateNodeStyle(node, _eachProperty);\n            }\n          } else {\n            await updateNodeStyle(node, _property);\n          }\n        }\n      }\n    }\n  };\n}\n\nasync function updateNodeStyle(node: Element, property: string) {\n  if (node?.properties?.['style'] && property) {\n    const cssObject =\n      postcssJs.objectify(postcss.parse(node.properties['style'])) || {};\n    delete cssObject[property];\n\n    const result = await postcss().process(cssObject, {\n      // @ts-expect-error 见 https://github.com/postcss/postcss-js#compile-css-in-js-to-css\n      parser: postcssJs,\n    });\n\n    const cssString = result.css;\n\n    if (cssString && node?.properties?.['style']) {\n      node.properties['style'] = cssString;\n    } else {\n      delete node.properties?.['style'];\n    }\n  }\n\n  if (node.properties?.[property] && property) {\n    delete node.properties[property];\n  }\n}\n","import type * as hast from 'hast';\nimport type { Transformer } from 'unified';\nimport { visit } from 'unist-util-visit';\n\nexport function rehypeRemoveDumiWrapper(): Transformer {\n  return (tree) => {\n    visit(tree, 'element', (node: hast.Element, _, parent: hast.Parent) => {\n      if (node.properties && node.properties['typography'] === 'classic') {\n        parent.children = node.children;\n      }\n    });\n  };\n}\n","import type { IRoute } from 'dumi';\nimport type * as hast from 'hast';\nimport type * as mdx from 'mdast-util-mdx';\nimport type { Transformer } from 'unified';\nimport { convert } from 'unist-util-is';\nimport { SKIP, visit } from 'unist-util-visit';\n\nexport type RouteMappingOptions = {\n  /**\n   * Regular expression to match the links you want to rewrite. The regex MUST have a\n   * capture group, and the capture group MUST capture the route ID.\n   */\n  match: RegExp;\n  /**\n   * Function to extract the destination path from Dumi's router.\n   *\n   * @param match The route ID from the capture group in `match`\n   * @param path The candidate path\n   * @returns Whether the path is the correct destination\n   */\n  resolve?: (match: string, path: string) => boolean;\n  routes: Record<string, IRoute>;\n};\n\n/**\n * Rewrite all <a>s to <Link>s that point to Dumi routes.\n *\n * Caveat: The resulting <Link>s are in fact HAST Elements, not MDAST PhrasingContent.\n */\nexport function rehypeRewriteLinks(\n  { match, resolve, routes }: RouteMappingOptions = {\n    match: /(.*)/,\n    routes: {},\n  },\n): Transformer {\n  return async (tree, file) => {\n    visit(\n      tree,\n      convert((node) => {\n        if (node.type !== 'element') {\n          return false;\n        }\n        if ((node as hast.Element).tagName !== 'a') {\n          return false;\n        }\n        if ((node as hast.Element).properties?.['download']) {\n          return false;\n        }\n        if ((node as hast.Element).properties?.['href'] === undefined) {\n          return false;\n        }\n        return true;\n      }),\n      (node, idx: number | null, parent: hast.Parent) => {\n        if (idx === null) {\n          return;\n        }\n        const href = (node as hast.Element).properties?.['href'];\n        if (typeof href !== 'string') {\n          return;\n        }\n\n        try {\n          new URL(href, 'http://example.com');\n        } catch (e) {\n          return;\n        }\n\n        const { origin, pathname, search, hash } = new URL(href, 'http://example.com');\n        const location = `${origin}${pathname}`.replace(/^http:\\/\\/example\\.com/, '');\n        const id = location.match(match)?.[1];\n\n        if (id === undefined) {\n          return;\n        }\n\n        const routeMap = routes;\n\n        let route: IRoute | undefined;\n        if (resolve !== undefined) {\n          route = Object.values(routeMap).find((r) => resolve(id, r.path));\n        } else {\n          route = routeMap[id];\n        }\n\n        let anchor: mdx.MdxJsxTextElement;\n\n        const props = (node as hast.Element).properties ?? {};\n\n        if (route === undefined) {\n          return;\n        } else if (route.file === file.path) {\n          // in-page fragment redirect\n          props['href'] = hash;\n          anchor = {\n            type: 'mdxJsxTextElement',\n            name: 'a',\n            attributes: Object.entries(props).map(\n              ([name, value]): mdx.MdxJsxAttribute => ({\n                type: 'mdxJsxAttribute',\n                name,\n                value: String(value),\n              }),\n            ),\n            // @ts-expect-error We are putting HAST nodes in MDX\n            children: (node as hast.Element).children,\n          };\n        } else {\n          // cross-page redirect\n          delete props['href'];\n          props['to'] = `${route.absPath}${search}${hash}`;\n          anchor = {\n            type: 'mdxJsxTextElement',\n            name: 'Link',\n            attributes: Object.entries(props).map(\n              ([name, value]): mdx.MdxJsxAttribute => ({\n                type: 'mdxJsxAttribute',\n                name,\n                value: String(value),\n              }),\n            ),\n            // @ts-expect-error We are putting HAST nodes in MDX\n            children: (node as hast.Element).children,\n          };\n        }\n\n        // @ts-expect-error We are putting a JSX element inside a HAST\n        parent.children[idx] = anchor;\n        return SKIP;\n      },\n    );\n  };\n}\n"],"mappings":";AAAA,SAAS,qBAAqB;AAE9B,SAAS,iBAAiB;AAC1B,SAAS,4BAA4B;AACrC,SAAS,yBAAyB;AAClC,SAAS,gCAAgC;AACzC,SAAS,wBAAwB;AACjC,SAAS,iCAAiC;AAC1C,SAAS,+BAA+B;AACxC,SAAS,sBAAsB;AAE/B,OAAO,eAAe;AACtB,OAAO,8BAA8B;AACrC,OAAO,uBAAuB;AAC9B,OAAO,UAAU;;;ACZjB,SAAS,aAAa;AAEf,SAAS,0BAAuC;AACrD,SAAO,CAAC,SAAS;AACf,UAAM,MAAM,WAAW,CAAC,SAAkB;AACxC;AAAA,MAGA;AACA,UAAI,KAAK,YAAY,OAAO;AAC1B,YAAI,CAAC,KAAK,YAAY;AACpB,eAAK,aAAa,CAAC;AACnB,eAAK,WAAW,gBAAgB,IAAI;AAAA,QACtC,OAAO;AACL,cAAI,CAAC,KAAK,WAAW,gBAAgB,GAAG;AACtC,iBAAK,WAAW,gBAAgB,IAAI;AAAA,UACtC;AAAA,QACF;AAAA,MACF;AAAA,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