🎉 initiate project *astro_rewrite*

This commit is contained in:
sindrekjelsrud 2023-07-19 21:31:30 +02:00
parent ffd4d5e86c
commit 2ba37bfbe3
8658 changed files with 2268794 additions and 2538 deletions

View file

@ -0,0 +1,7 @@
import type { Root } from 'hast';
import type MagicString from 'magic-string';
import type { Plugin } from 'unified';
declare const rehypeEscape: Plugin<[{
s: MagicString;
}], Root>;
export default rehypeEscape;

View file

@ -0,0 +1,25 @@
import { visit } from "unist-util-visit";
import { escape, needsEscape, replaceAttribute } from "./utils.js";
const rehypeEscape = ({ s }) => {
return (tree) => {
visit(tree, (node) => {
if (node.type === "text" || node.type === "comment") {
if (needsEscape(node.value)) {
s.overwrite(node.position.start.offset, node.position.end.offset, escape(node.value));
}
} else if (node.type === "element") {
for (const [key, value] of Object.entries(node.properties ?? {})) {
const newKey = needsEscape(key) ? escape(key) : key;
const newValue = needsEscape(value) ? escape(value) : value;
if (newKey === key && newValue === value)
continue;
replaceAttribute(s, node, key, value === "" ? newKey : `${newKey}="${newValue}"`);
}
}
});
};
};
var escape_default = rehypeEscape;
export {
escape_default as default
};

View file

@ -0,0 +1,4 @@
export declare function transform(code: string, id: string): Promise<{
code: string;
map: import("magic-string").SourceMap;
}>;

View file

@ -0,0 +1,21 @@
import MagicString from "magic-string";
import { rehype } from "rehype";
import { VFile } from "vfile";
import escape from "./escape.js";
import slots, { SLOT_PREFIX } from "./slots.js";
async function transform(code, id) {
const s = new MagicString(code, { filename: id });
const parser = rehype().data("settings", { fragment: true }).use(escape, { s }).use(slots, { s });
const vfile = new VFile({ value: code, path: id });
await parser.process(vfile);
s.prepend(`function render({ slots: ${SLOT_PREFIX} }) {
return \``);
s.append('`\n }\nrender["astro:html"] = true;\nexport default render;');
return {
code: s.toString(),
map: s.generateMap()
};
}
export {
transform
};

View file

@ -0,0 +1,8 @@
import type { Root } from 'hast';
import type { Plugin } from 'unified';
import type MagicString from 'magic-string';
declare const rehypeSlots: Plugin<[{
s: MagicString;
}], Root>;
export default rehypeSlots;
export declare const SLOT_PREFIX = "___SLOTS___";

View file

@ -0,0 +1,26 @@
import { visit } from "unist-util-visit";
import { escape } from "./utils.js";
const rehypeSlots = ({ s }) => {
return (tree, file) => {
visit(tree, (node) => {
var _a, _b, _c, _d, _e, _f;
if (node.type === "element" && node.tagName === "slot") {
if (typeof ((_a = node.properties) == null ? void 0 : _a["is:inline"]) !== "undefined")
return;
const name = ((_b = node.properties) == null ? void 0 : _b["name"]) ?? "default";
const start = ((_c = node.position) == null ? void 0 : _c.start.offset) ?? 0;
const end = ((_d = node.position) == null ? void 0 : _d.end.offset) ?? 0;
const first = node.children.at(0) ?? node;
const last = node.children.at(-1) ?? node;
const text = file.value.slice(((_e = first.position) == null ? void 0 : _e.start.offset) ?? 0, ((_f = last.position) == null ? void 0 : _f.end.offset) ?? 0).toString();
s.overwrite(start, end, `\${${SLOT_PREFIX}["${name}"] ?? \`${escape(text).trim()}\`}`);
}
});
};
};
var slots_default = rehypeSlots;
const SLOT_PREFIX = `___SLOTS___`;
export {
SLOT_PREFIX,
slots_default as default
};

View file

@ -0,0 +1,5 @@
import type { Element } from 'hast';
import type MagicString from 'magic-string';
export declare function replaceAttribute(s: MagicString, node: Element, key: string, newValue: string): void;
export declare function needsEscape(value: any): value is string;
export declare function escape(value: string): string;

View file

@ -0,0 +1,30 @@
const splitAttrsTokenizer = /([\$\{\}\@a-z0-9_\:\-]*)\s*?=\s*?(['"]?)(.*?)\2\s+/gim;
function replaceAttribute(s, node, key, newValue) {
var _a, _b;
splitAttrsTokenizer.lastIndex = 0;
const text = s.original.slice(((_a = node.position) == null ? void 0 : _a.start.offset) ?? 0, ((_b = node.position) == null ? void 0 : _b.end.offset) ?? 0).toString();
const offset = text.indexOf(key);
if (offset === -1)
return;
const start = node.position.start.offset + offset;
const tokens = text.slice(offset).split(splitAttrsTokenizer);
const token = tokens[0].replace(/([^>])(\>[\s\S]*$)/gim, "$1");
if (token.trim() === key) {
const end = start + key.length;
s.overwrite(start, end, newValue);
} else {
const end = start + `${key}=${tokens[2]}${tokens[3]}${tokens[2]}`.length;
s.overwrite(start, end, newValue);
}
}
function needsEscape(value) {
return typeof value === "string" && (value.includes("`") || value.includes("${"));
}
function escape(value) {
return value.replace(/`/g, "\\`").replace(/\$\{/g, "\\${");
}
export {
escape,
needsEscape,
replaceAttribute
};