mirror of
https://github.com/withastro/astro.git
synced 2025-02-03 22:29:08 -05:00
[ci] format
This commit is contained in:
parent
e6945bcf23
commit
659087be1a
3 changed files with 14 additions and 6 deletions
|
@ -10,12 +10,16 @@ const rehypeEscape: Plugin<[{ s: MagicString }], Root> = ({ s }) => {
|
||||||
visit(tree, (node: Root | RootContent) => {
|
visit(tree, (node: Root | RootContent) => {
|
||||||
if (node.type === 'text' || node.type === 'comment') {
|
if (node.type === 'text' || node.type === 'comment') {
|
||||||
if (needsEscape(node.value)) {
|
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') {
|
} else if (node.type === 'element') {
|
||||||
if (!node.properties) return;
|
if (!node.properties) return;
|
||||||
for (let [key, value] of Object.entries(node.properties)) {
|
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 newKey = needsEscape(key) ? escapeTemplateLiteralCharacters(key) : key;
|
||||||
const newValue = needsEscape(value) ? escapeTemplateLiteralCharacters(value) : value;
|
const newValue = needsEscape(value) ? escapeTemplateLiteralCharacters(value) : value;
|
||||||
if (newKey === key && newValue === value) continue;
|
if (newKey === key && newValue === value) continue;
|
||||||
|
|
|
@ -18,7 +18,11 @@ const rehypeSlots: Plugin<[{ s: MagicString }], Root> = ({ s }) => {
|
||||||
const text = file.value
|
const text = file.value
|
||||||
.slice(first.position?.start.offset ?? 0, last.position?.end.offset ?? 0)
|
.slice(first.position?.start.offset ?? 0, last.position?.end.offset ?? 0)
|
||||||
.toString();
|
.toString();
|
||||||
s.overwrite(start, end, `\${${SLOT_PREFIX}["${name}"] ?? \`${escapeTemplateLiteralCharacters(text).trim()}\`}`);
|
s.overwrite(
|
||||||
|
start,
|
||||||
|
end,
|
||||||
|
`\${${SLOT_PREFIX}["${name}"] ?? \`${escapeTemplateLiteralCharacters(text).trim()}\`}`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -25,7 +25,7 @@ export function replaceAttribute(s: MagicString, node: Element, key: string, new
|
||||||
|
|
||||||
// Embedding in our own template literal expression requires escaping
|
// Embedding in our own template literal expression requires escaping
|
||||||
// any meaningful template literal characters in the user's code!
|
// 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 {
|
export function needsEscape(value: any): value is string {
|
||||||
// Reset the RegExp's global state
|
// Reset the RegExp's global state
|
||||||
|
@ -44,7 +44,7 @@ export function escapeTemplateLiteralCharacters(value: string) {
|
||||||
|
|
||||||
// Rather than a naive `String.replace()`, we have to iterate through
|
// Rather than a naive `String.replace()`, we have to iterate through
|
||||||
// the raw contents to properly handle existing backslashes
|
// 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
|
// Final loop when char === undefined, append trailing content
|
||||||
if (!char) {
|
if (!char) {
|
||||||
text += value.slice(startIndex);
|
text += value.slice(startIndex);
|
||||||
|
|
Loading…
Add table
Reference in a new issue