🎉 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,8 @@
import type { VFile, VFileData as Data } from 'vfile';
import type { MarkdownAstroData } from './types.js';
export declare class InvalidAstroDataError extends TypeError {
}
export declare function safelyGetAstroData(vfileData: Data): MarkdownAstroData | InvalidAstroDataError;
export declare function toRemarkInitializeAstroData({ userFrontmatter, }: {
userFrontmatter: Record<string, any>;
}): () => (tree: any, vfile: VFile) => void;

View file

@ -0,0 +1,35 @@
function isValidAstroData(obj) {
if (typeof obj === "object" && obj !== null && obj.hasOwnProperty("frontmatter")) {
const { frontmatter } = obj;
try {
JSON.stringify(frontmatter);
} catch {
return false;
}
return typeof frontmatter === "object" && frontmatter !== null;
}
return false;
}
class InvalidAstroDataError extends TypeError {
}
function safelyGetAstroData(vfileData) {
const { astro } = vfileData;
if (!astro || !isValidAstroData(astro)) {
return new InvalidAstroDataError();
}
return astro;
}
function toRemarkInitializeAstroData({
userFrontmatter
}) {
return () => function(tree, vfile) {
if (!vfile.data.astro) {
vfile.data.astro = { frontmatter: userFrontmatter };
}
};
}
export {
InvalidAstroDataError,
safelyGetAstroData,
toRemarkInitializeAstroData
};

View file

@ -0,0 +1,7 @@
import type { AstroMarkdownOptions, MarkdownRenderingOptions, MarkdownRenderingResult } from './types';
export { rehypeHeadingIds } from './rehype-collect-headings.js';
export { remarkCollectImages } from './remark-collect-images.js';
export * from './types.js';
export declare const markdownConfigDefaults: Omit<Required<AstroMarkdownOptions>, 'drafts'>;
/** Shared utility for rendering markdown */
export declare function renderMarkdown(content: string, opts: MarkdownRenderingOptions): Promise<MarkdownRenderingResult>;

133
node_modules/@astrojs/markdown-remark/dist/index.js generated vendored Normal file
View file

@ -0,0 +1,133 @@
import { toRemarkInitializeAstroData } from "./frontmatter-injection.js";
import { loadPlugins } from "./load-plugins.js";
import { rehypeHeadingIds } from "./rehype-collect-headings.js";
import { remarkCollectImages } from "./remark-collect-images.js";
import remarkPrism from "./remark-prism.js";
import scopedStyles from "./remark-scoped-styles.js";
import remarkShiki from "./remark-shiki.js";
import rehypeRaw from "rehype-raw";
import rehypeStringify from "rehype-stringify";
import remarkGfm from "remark-gfm";
import markdown from "remark-parse";
import markdownToHtml from "remark-rehype";
import remarkSmartypants from "remark-smartypants";
import { unified } from "unified";
import { VFile } from "vfile";
import { rehypeImages } from "./rehype-images.js";
import { rehypeHeadingIds as rehypeHeadingIds2 } from "./rehype-collect-headings.js";
import { remarkCollectImages as remarkCollectImages2 } from "./remark-collect-images.js";
export * from "./types.js";
const markdownConfigDefaults = {
syntaxHighlight: "shiki",
shikiConfig: {
langs: [],
theme: "github-dark",
wrap: false
},
remarkPlugins: [],
rehypePlugins: [],
remarkRehype: {},
gfm: true,
smartypants: true
};
const isPerformanceBenchmark = Boolean(process.env.ASTRO_PERFORMANCE_BENCHMARK);
async function renderMarkdown(content, opts) {
var _a;
let {
fileURL,
syntaxHighlight = markdownConfigDefaults.syntaxHighlight,
shikiConfig = markdownConfigDefaults.shikiConfig,
remarkPlugins = markdownConfigDefaults.remarkPlugins,
rehypePlugins = markdownConfigDefaults.rehypePlugins,
remarkRehype = markdownConfigDefaults.remarkRehype,
gfm = markdownConfigDefaults.gfm,
smartypants = markdownConfigDefaults.smartypants,
frontmatter: userFrontmatter = {}
} = opts;
const input = new VFile({ value: content, path: fileURL });
const scopedClassName = (_a = opts.$) == null ? void 0 : _a.scopedClassName;
let parser = unified().use(markdown).use(toRemarkInitializeAstroData({ userFrontmatter })).use([]);
if (!isPerformanceBenchmark && gfm) {
if (gfm) {
parser.use(remarkGfm);
}
if (smartypants) {
parser.use(remarkSmartypants);
}
}
const loadedRemarkPlugins = await Promise.all(loadPlugins(remarkPlugins));
const loadedRehypePlugins = await Promise.all(loadPlugins(rehypePlugins));
loadedRemarkPlugins.forEach(([plugin, pluginOpts]) => {
parser.use([[plugin, pluginOpts]]);
});
if (!isPerformanceBenchmark) {
if (scopedClassName) {
parser.use([scopedStyles(scopedClassName)]);
}
if (syntaxHighlight === "shiki") {
parser.use([await remarkShiki(shikiConfig, scopedClassName)]);
} else if (syntaxHighlight === "prism") {
parser.use([remarkPrism(scopedClassName)]);
}
if (opts.experimentalAssets) {
parser.use([remarkCollectImages]);
}
}
parser.use([
[
markdownToHtml,
{
allowDangerousHtml: true,
passThrough: [],
...remarkRehype
}
]
]);
loadedRehypePlugins.forEach(([plugin, pluginOpts]) => {
parser.use([[plugin, pluginOpts]]);
});
if (opts.experimentalAssets) {
parser.use(rehypeImages());
}
if (!isPerformanceBenchmark) {
parser.use([rehypeHeadingIds]);
}
parser.use([rehypeRaw]).use(rehypeStringify, { allowDangerousHtml: true });
let vfile;
try {
vfile = await parser.process(input);
} catch (err) {
err = prefixError(err, `Failed to parse Markdown file "${input.path}"`);
console.error(err);
throw err;
}
const headings = (vfile == null ? void 0 : vfile.data.__astroHeadings) || [];
return {
metadata: { headings, source: content, html: String(vfile.value) },
code: String(vfile.value),
vfile
};
}
function prefixError(err, prefix) {
if (err && err.message) {
try {
err.message = `${prefix}:
${err.message}`;
return err;
} catch (error) {
}
}
const wrappedError = new Error(`${prefix}${err ? `: ${err}` : ""}`);
try {
wrappedError.stack = err.stack;
wrappedError.cause = err;
} catch (error) {
}
return wrappedError;
}
export {
markdownConfigDefaults,
rehypeHeadingIds2 as rehypeHeadingIds,
remarkCollectImages2 as remarkCollectImages,
renderMarkdown
};

View file

@ -0,0 +1 @@
export { InvalidAstroDataError, safelyGetAstroData, toRemarkInitializeAstroData, } from './frontmatter-injection.js';

10
node_modules/@astrojs/markdown-remark/dist/internal.js generated vendored Normal file
View file

@ -0,0 +1,10 @@
import {
InvalidAstroDataError,
safelyGetAstroData,
toRemarkInitializeAstroData
} from "./frontmatter-injection.js";
export {
InvalidAstroDataError,
safelyGetAstroData,
toRemarkInitializeAstroData
};

View file

@ -0,0 +1,2 @@
import type * as unified from 'unified';
export declare function loadPlugins(items: (string | [string, any] | unified.Plugin<any[], any> | [unified.Plugin<any[], any>, any])[]): Promise<[unified.Plugin, any?]>[];

View file

@ -0,0 +1,31 @@
import { resolve as importMetaResolve } from "import-meta-resolve";
import path from "path";
import { pathToFileURL } from "url";
const cwdUrlStr = pathToFileURL(path.join(process.cwd(), "package.json")).toString();
async function importPlugin(p) {
if (typeof p === "string") {
try {
const importResult2 = await import(p);
return importResult2.default;
} catch {
}
const resolved = await importMetaResolve(p, cwdUrlStr);
const importResult = await import(resolved);
return importResult.default;
}
return p;
}
function loadPlugins(items) {
return items.map((p) => {
return new Promise((resolve, reject) => {
if (Array.isArray(p)) {
const [plugin, opts] = p;
return importPlugin(plugin).then((m) => resolve([m, opts])).catch((e) => reject(e));
}
return importPlugin(p).then((m) => resolve([m])).catch((e) => reject(e));
});
});
}
export {
loadPlugins
};

View file

@ -0,0 +1,2 @@
import type { RehypePlugin } from './types.js';
export declare function rehypeHeadingIds(): ReturnType<RehypePlugin>;

View file

@ -0,0 +1,99 @@
import Slugger from "github-slugger";
import { visit } from "unist-util-visit";
import { InvalidAstroDataError, safelyGetAstroData } from "./frontmatter-injection.js";
const rawNodeTypes = /* @__PURE__ */ new Set(["text", "raw", "mdxTextExpression"]);
const codeTagNames = /* @__PURE__ */ new Set(["code", "pre"]);
function rehypeHeadingIds() {
return function(tree, file) {
const headings = [];
const slugger = new Slugger();
const isMDX = isMDXFile(file);
const astroData = safelyGetAstroData(file.data);
visit(tree, (node) => {
if (node.type !== "element")
return;
const { tagName } = node;
if (tagName[0] !== "h")
return;
const [_, level] = tagName.match(/h([0-6])/) ?? [];
if (!level)
return;
const depth = Number.parseInt(level);
let text = "";
visit(node, (child, __, parent) => {
if (child.type === "element" || parent == null) {
return;
}
if (child.type === "raw") {
if (child.value.match(/^\n?<.*>\n?$/)) {
return;
}
}
if (rawNodeTypes.has(child.type)) {
if (isMDX || codeTagNames.has(parent.tagName)) {
let value = child.value;
if (isMdxTextExpression(child) && !(astroData instanceof InvalidAstroDataError)) {
const frontmatterPath = getMdxFrontmatterVariablePath(child);
if (Array.isArray(frontmatterPath) && frontmatterPath.length > 0) {
const frontmatterValue = getMdxFrontmatterVariableValue(astroData, frontmatterPath);
if (typeof frontmatterValue === "string") {
value = frontmatterValue;
}
}
}
text += value;
} else {
text += child.value.replace(/\{/g, "${");
}
}
});
node.properties = node.properties || {};
if (typeof node.properties.id !== "string") {
let slug = slugger.slug(text);
if (slug.endsWith("-"))
slug = slug.slice(0, -1);
node.properties.id = slug;
}
headings.push({ depth, slug: node.properties.id, text });
});
file.data.__astroHeadings = headings;
};
}
function isMDXFile(file) {
var _a;
return Boolean((_a = file.history[0]) == null ? void 0 : _a.endsWith(".mdx"));
}
function getMdxFrontmatterVariablePath(node) {
var _a;
if (!((_a = node.data) == null ? void 0 : _a.estree) || node.data.estree.body.length !== 1)
return new Error();
const statement = node.data.estree.body[0];
if ((statement == null ? void 0 : statement.type) !== "ExpressionStatement" || statement.expression.type !== "MemberExpression")
return new Error();
let expression = statement.expression;
const expressionPath = [];
while (expression.type === "MemberExpression" && expression.property.type === (expression.computed ? "Literal" : "Identifier")) {
expressionPath.push(
expression.property.type === "Literal" ? String(expression.property.value) : expression.property.name
);
expression = expression.object;
}
if (expression.type !== "Identifier" || expression.name !== "frontmatter")
return new Error();
return expressionPath.reverse();
}
function getMdxFrontmatterVariableValue(astroData, path) {
let value = astroData.frontmatter;
for (const key of path) {
if (!value[key])
return void 0;
value = value[key];
}
return value;
}
function isMdxTextExpression(node) {
return node.type === "mdxTextExpression";
}
export {
rehypeHeadingIds
};

View file

@ -0,0 +1,2 @@
import type { MarkdownVFile } from './types.js';
export declare function rehypeImages(): () => (tree: any, file: MarkdownVFile) => void;

View file

@ -0,0 +1,21 @@
import { visit } from "unist-util-visit";
function rehypeImages() {
return () => function(tree, file) {
visit(tree, (node) => {
var _a, _b;
if (node.type !== "element")
return;
if (node.tagName !== "img")
return;
if ((_a = node.properties) == null ? void 0 : _a.src) {
if ((_b = file.data.imagePaths) == null ? void 0 : _b.has(node.properties.src)) {
node.properties["__ASTRO_IMAGE_"] = node.properties.src;
delete node.properties.src;
}
}
});
};
}
export {
rehypeImages
};

View file

@ -0,0 +1,2 @@
import type { MarkdownVFile } from './types';
export declare function remarkCollectImages(): (tree: any, vfile: MarkdownVFile) => void;

View file

@ -0,0 +1,27 @@
import { visit } from "unist-util-visit";
function remarkCollectImages() {
return function(tree, vfile) {
if (typeof (vfile == null ? void 0 : vfile.path) !== "string")
return;
const imagePaths = /* @__PURE__ */ new Set();
visit(tree, "image", (node) => {
if (shouldOptimizeImage(node.url))
imagePaths.add(node.url);
});
vfile.data.imagePaths = imagePaths;
};
}
function shouldOptimizeImage(src) {
return !isValidUrl(src) && !src.startsWith("/");
}
function isValidUrl(str) {
try {
new URL(str);
return true;
} catch {
return false;
}
}
export {
remarkCollectImages
};

View file

@ -0,0 +1,3 @@
type MaybeString = string | null | undefined;
declare function plugin(className: MaybeString): () => (tree: any) => void;
export default plugin;

View file

@ -0,0 +1,28 @@
import { runHighlighterWithAstro } from "@astrojs/prism/dist/highlighter";
import { visit } from "unist-util-visit";
const noVisit = /* @__PURE__ */ new Set(["root", "html", "text"]);
function transformer(className) {
return function(tree) {
const visitor = (node) => {
let { lang, value } = node;
node.type = "html";
let { html, classLanguage } = runHighlighterWithAstro(lang, value);
let classes = [classLanguage];
if (className) {
classes.push(className);
}
node.value = `<pre class="${classes.join(
" "
)}"><code is:raw class="${classLanguage}">${html}</code></pre>`;
return node;
};
return visit(tree, "code", visitor);
};
}
function plugin(className) {
return transformer.bind(null, className);
}
var remark_prism_default = plugin;
export {
remark_prism_default as default
};

View file

@ -0,0 +1,2 @@
/** */
export default function scopedStyles(className: string): () => (tree: any) => void;

View file

@ -0,0 +1,19 @@
import { visit } from "unist-util-visit";
const noVisit = /* @__PURE__ */ new Set(["root", "html", "text"]);
function scopedStyles(className) {
const visitor = (node) => {
var _a;
if (noVisit.has(node.type))
return;
const { data } = node;
let currentClassName = ((_a = data == null ? void 0 : data.hProperties) == null ? void 0 : _a.class) ?? "";
node.data = node.data || {};
node.data.hProperties = node.data.hProperties || {};
node.data.hProperties.class = `${className} ${currentClassName}`.trim();
return node;
};
return () => (tree) => visit(tree, visitor);
}
export {
scopedStyles as default
};

View file

@ -0,0 +1,3 @@
import type { ShikiConfig } from './types.js';
declare const remarkShiki: ({ langs, theme, wrap }: ShikiConfig, scopedClassName?: string | null) => Promise<() => (tree: any) => void>;
export default remarkShiki;

View file

@ -0,0 +1,92 @@
import { getHighlighter } from "shiki";
import { visit } from "unist-util-visit";
const highlighterCacheAsync = /* @__PURE__ */ new Map();
const compatThemes = {
"material-darker": "material-theme-darker",
"material-default": "material-theme",
"material-lighter": "material-theme-lighter",
"material-ocean": "material-theme-ocean",
"material-palenight": "material-theme-palenight"
};
const normalizeTheme = (theme) => {
if (typeof theme === "string") {
return compatThemes[theme] || theme;
} else if (compatThemes[theme.name]) {
return { ...theme, name: compatThemes[theme.name] };
} else {
return theme;
}
};
const remarkShiki = async ({ langs = [], theme = "github-dark", wrap = false }, scopedClassName) => {
theme = normalizeTheme(theme);
const cacheID = typeof theme === "string" ? theme : theme.name;
let highlighterAsync = highlighterCacheAsync.get(cacheID);
if (!highlighterAsync) {
highlighterAsync = getHighlighter({ theme }).then((hl) => {
hl.setColorReplacements({
"#000001": "var(--astro-code-color-text)",
"#000002": "var(--astro-code-color-background)",
"#000004": "var(--astro-code-token-constant)",
"#000005": "var(--astro-code-token-string)",
"#000006": "var(--astro-code-token-comment)",
"#000007": "var(--astro-code-token-keyword)",
"#000008": "var(--astro-code-token-parameter)",
"#000009": "var(--astro-code-token-function)",
"#000010": "var(--astro-code-token-string-expression)",
"#000011": "var(--astro-code-token-punctuation)",
"#000012": "var(--astro-code-token-link)"
});
return hl;
});
highlighterCacheAsync.set(cacheID, highlighterAsync);
}
const highlighter = await highlighterAsync;
for (const lang of langs) {
await highlighter.loadLanguage(lang);
}
return () => (tree) => {
visit(tree, "code", (node) => {
let lang;
if (typeof node.lang === "string") {
const langExists = highlighter.getLoadedLanguages().includes(node.lang);
if (langExists) {
lang = node.lang;
} else {
console.warn(`The language "${node.lang}" doesn't exist, falling back to plaintext.`);
lang = "plaintext";
}
} else {
lang = "plaintext";
}
let html = highlighter.codeToHtml(node.value, { lang });
html = html.replace(
/<pre class="(.*?)shiki(.*?)"/,
`<pre is:raw class="$1astro-code$2${scopedClassName ? " " + scopedClassName : ""}"`
);
if (node.lang === "diff") {
html = html.replace(
/<span class="line"><span style="(.*?)">([\+|\-])/g,
'<span class="line"><span style="$1"><span style="user-select: none;">$2</span>'
);
}
if (wrap === false) {
html = html.replace(/style="(.*?)"/, 'style="$1; overflow-x: auto;"');
} else if (wrap === true) {
html = html.replace(
/style="(.*?)"/,
'style="$1; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word;"'
);
}
if (scopedClassName) {
html = html.replace(/\<span class="line"\>/g, `<span class="line ${scopedClassName}"`);
}
node.type = "html";
node.value = html;
node.children = [];
});
};
};
var remark_shiki_default = remarkShiki;
export {
remark_shiki_default as default
};

71
node_modules/@astrojs/markdown-remark/dist/types.d.ts generated vendored Normal file
View file

@ -0,0 +1,71 @@
import type * as hast from 'hast';
import type * as mdast from 'mdast';
import type { all as Handlers, one as Handler, Options as RemarkRehypeOptions } from 'remark-rehype';
import type { ILanguageRegistration, IThemeRegistration, Theme } from 'shiki';
import type * as unified from 'unified';
import type { VFile } from 'vfile';
export type { Node } from 'unist';
export type MarkdownAstroData = {
frontmatter: Record<string, any>;
};
export type RemarkPlugin<PluginParameters extends any[] = any[]> = unified.Plugin<PluginParameters, mdast.Root>;
export type RemarkPlugins = (string | [string, any] | RemarkPlugin | [RemarkPlugin, any])[];
export type RehypePlugin<PluginParameters extends any[] = any[]> = unified.Plugin<PluginParameters, hast.Root>;
export type RehypePlugins = (string | [string, any] | RehypePlugin | [RehypePlugin, any])[];
export type RemarkRehype = Omit<RemarkRehypeOptions, 'handlers' | 'unknownHandler'> & {
handlers?: typeof Handlers;
handler?: typeof Handler;
};
export interface ShikiConfig {
langs?: ILanguageRegistration[];
theme?: Theme | IThemeRegistration;
wrap?: boolean | null;
}
export interface AstroMarkdownOptions {
drafts?: boolean;
syntaxHighlight?: 'shiki' | 'prism' | false;
shikiConfig?: ShikiConfig;
remarkPlugins?: RemarkPlugins;
rehypePlugins?: RehypePlugins;
remarkRehype?: RemarkRehype;
gfm?: boolean;
smartypants?: boolean;
}
export interface ImageMetadata {
src: string;
width: number;
height: number;
type: string;
}
export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
/** @internal */
fileURL?: URL;
/** @internal */
$?: {
scopedClassName: string | null;
};
/** Used for frontmatter injection plugins */
frontmatter?: Record<string, any>;
experimentalAssets?: boolean;
}
export interface MarkdownHeading {
depth: number;
slug: string;
text: string;
}
export interface MarkdownMetadata {
headings: MarkdownHeading[];
source: string;
html: string;
}
export interface MarkdownVFile extends VFile {
data: {
__astroHeadings?: MarkdownHeading[];
imagePaths?: Set<string>;
};
}
export interface MarkdownRenderingResult {
metadata: MarkdownMetadata;
vfile: MarkdownVFile;
code: string;
}

0
node_modules/@astrojs/markdown-remark/dist/types.js generated vendored Normal file
View file