0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-20 22:12:38 -05:00

[ci] format

This commit is contained in:
cin 2024-01-23 23:47:45 +00:00 committed by astrobot-houston
parent e6945bcf23
commit 659087be1a
3 changed files with 14 additions and 6 deletions

View file

@ -10,12 +10,16 @@ const rehypeEscape: Plugin<[{ s: MagicString }], Root> = ({ s }) => {
visit(tree, (node: Root | RootContent) => {
if (node.type === 'text' || node.type === 'comment') {
if (needsEscape(node.value)) {
s.overwrite(node.position!.start.offset!, node.position!.end.offset!, escapeTemplateLiteralCharacters(node.value));
s.overwrite(
node.position!.start.offset!,
node.position!.end.offset!,
escapeTemplateLiteralCharacters(node.value)
);
}
} else if (node.type === 'element') {
if (!node.properties) return;
for (let [key, value] of Object.entries(node.properties)) {
key = key.replace(/([A-Z])/g, '-$1').toLowerCase()
key = key.replace(/([A-Z])/g, '-$1').toLowerCase();
const newKey = needsEscape(key) ? escapeTemplateLiteralCharacters(key) : key;
const newValue = needsEscape(value) ? escapeTemplateLiteralCharacters(value) : value;
if (newKey === key && newValue === value) continue;

View file

@ -18,7 +18,11 @@ const rehypeSlots: Plugin<[{ s: MagicString }], Root> = ({ s }) => {
const text = file.value
.slice(first.position?.start.offset ?? 0, last.position?.end.offset ?? 0)
.toString();
s.overwrite(start, end, `\${${SLOT_PREFIX}["${name}"] ?? \`${escapeTemplateLiteralCharacters(text).trim()}\`}`);
s.overwrite(
start,
end,
`\${${SLOT_PREFIX}["${name}"] ?? \`${escapeTemplateLiteralCharacters(text).trim()}\`}`
);
}
});
};

View file

@ -25,7 +25,7 @@ export function replaceAttribute(s: MagicString, node: Element, key: string, new
// Embedding in our own template literal expression requires escaping
// any meaningful template literal characters in the user's code!
const NEEDS_ESCAPE_RE = /[`\\]|\$\{/g
const NEEDS_ESCAPE_RE = /[`\\]|\$\{/g;
export function needsEscape(value: any): value is string {
// Reset the RegExp's global state
@ -41,10 +41,10 @@ export function escapeTemplateLiteralCharacters(value: string) {
let startIndex = 0;
let segment = '';
let text = '';
// Rather than a naive `String.replace()`, we have to iterate through
// the raw contents to properly handle existing backslashes
while ([char] = NEEDS_ESCAPE_RE.exec(value) ?? []) {
while (([char] = NEEDS_ESCAPE_RE.exec(value) ?? [])) {
// Final loop when char === undefined, append trailing content
if (!char) {
text += value.slice(startIndex);