Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复非组件属性value被替换为$u问题 #2173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/loaders/markdown/transformer/rehypeRaw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import type { IMdTransformerOptions } from '.';
let raw: typeof import('hast-util-raw').raw;
let visit: typeof import('unist-util-visit').visit;
const COMPONENT_NAME_REGEX = /(<)([A-Z][a-zA-Z\d]*)([\s|>])/g;
const COMPONENT_PROP_REGEX = /\s[a-z][a-z\d]*[A-Z]+[a-zA-Z\d]*(=|\s|>)/g;
const COMPONENT_PROP_REGEX =
/(<[^>]*\s)[a-z][a-z\d]*[A-Z]+[a-zA-Z\d]*(=|\s|>)/g;
const COMPONENT_STUB_ATTR = '$tag-name';
const PROP_STUB_ATTR = '-$u';
const PROP_STUB_ATTR_REGEX = new RegExp(
Expand Down Expand Up @@ -46,11 +47,22 @@ export default function rehypeRaw(opts: IRehypeRawOptions): Transformer<Root> {
);
// mark all camelCase props for all custom react component
// because the parse5 within hast-util-raw will lowercase all attr names
node.value = node.value.replace(COMPONENT_PROP_REGEX, (str) => {
return str.replace(
/[A-Z]/g,
(s) => `${PROP_STUB_ATTR}${s.toLowerCase()}`,
node.value = node.value.replace(COMPONENT_PROP_REGEX, (str, prefix) => {
const parts = str.match(
/(<[^>]*\s)([a-z][a-z\d]*)([A-Z]+[a-zA-Z\d]*)(=|\s|>)/,
);
if (parts) {
const propName = parts[2];
const camelCasePart = parts[3];
const suffix = parts[4];
const addon = camelCasePart
?.split(/(?=[A-Z])/)
?.map((s) => PROP_STUB_ATTR + s?.toLowerCase())
?.join('');
const newPropName = propName + addon;
return prefix + newPropName + suffix;
}
return str;
});
} else if (node.type === 'element' && node.data?.meta) {
// save code meta to properties to avoid lost
Expand Down
Loading