mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 19:49:50 +02:00
feat: see macro
This commit is contained in:
1 parent
824169fc25
commit
cef18d4f64
9 files changed
+67
-18
No files matched your search
@@ -1,16 +1,45 @@
|
|||||||
export function literals(
|
export function literals(
|
||||||
string: string,
|
string: string,
|
||||||
literals?: Record<string, string | number | undefined>
|
literals?: Record<string, string | number | undefined>,
|
||||||
) {
|
) {
|
||||||
for (const key in literals) {
|
for (const key in literals) {
|
||||||
const mutated = string.replaceAll(`{${key}}`, `${literals[key]}`);
|
string = string.replaceAll(`{${key}}`, `${literals[key]}`);
|
||||||
|
|
||||||
if (import.meta.env.DEV && mutated === string) {
|
|
||||||
console.warn(`Could not find literal {${key}} in string "${string}"`);
|
|
||||||
}
|
|
||||||
|
|
||||||
string = mutated;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function literalsArray<Type, Literal = Type | string>(
|
||||||
|
string: string,
|
||||||
|
literals?: Record<string, Literal>,
|
||||||
|
) {
|
||||||
|
if (!literals) return [string];
|
||||||
|
|
||||||
|
let array: (string | Literal)[] = [string];
|
||||||
|
|
||||||
|
for (const key in literals) {
|
||||||
|
const value = literals[key];
|
||||||
|
const next: (string | Literal)[] = [];
|
||||||
|
|
||||||
|
for (const chunk of array) {
|
||||||
|
if (typeof chunk !== "string") {
|
||||||
|
next.push(chunk);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const parts = chunk.split(`{${key}}`);
|
||||||
|
|
||||||
|
for (let index = 0; index < parts.length; index++) {
|
||||||
|
if (parts[index]) next.push(parts[index]);
|
||||||
|
|
||||||
|
if (index < parts.length - 1) {
|
||||||
|
next.push(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
array = next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array;
|
||||||
|
}
|
||||||
@@ -122,6 +122,10 @@
|
|||||||
"pb": "Protocol Buffer methods",
|
"pb": "Protocol Buffer methods",
|
||||||
"media": "Media methods",
|
"media": "Media methods",
|
||||||
"no_docs": "This method has no documentation.",
|
"no_docs": "This method has no documentation.",
|
||||||
"test": "Test"
|
"test": "Test",
|
||||||
|
|
||||||
|
"macros": {
|
||||||
|
"see": "See {path} {message}"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -4,12 +4,13 @@ import { MarkdownCustom } from "./MarkdownCustom";
|
|||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
body: string;
|
body: string;
|
||||||
|
locale: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const macroPattern = /@(\w+) (.+)/;
|
const macroPattern = /@(\w+) (.+)/;
|
||||||
const macros: Record<string, string> = {};
|
const macros: Record<string, string> = {};
|
||||||
|
|
||||||
const { body: bodyOriginal } = Astro.props;
|
const { body: bodyOriginal, locale } = Astro.props;
|
||||||
let body = "";
|
let body = "";
|
||||||
|
|
||||||
for (const line of bodyOriginal.split("\n")) {
|
for (const line of bodyOriginal.split("\n")) {
|
||||||
@@ -53,6 +54,6 @@ for (const line of bodyOriginal.split("\n")) {
|
|||||||
<div class="gutter"></div>
|
<div class="gutter"></div>
|
||||||
<div class="content">
|
<div class="content">
|
||||||
<MarkdownCustom {body} />
|
<MarkdownCustom {body} />
|
||||||
{"see" in macros && <ApiMacroSee value={macros.see} />}
|
{"see" in macros && <ApiMacroSee {locale} value={macros.see} />}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
@@ -23,5 +23,5 @@ const body = apiData.data.docs ?? strings.api.no_docs;
|
|||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<ApiTester client:idle {locale} path={apiData.id} />
|
<ApiTester client:idle {locale} path={apiData.id} />
|
||||||
<ApiDescription {body} />
|
<ApiDescription {locale} {body} />
|
||||||
</div>
|
</div>
|
||||||
@@ -1,13 +1,20 @@
|
|||||||
---
|
---
|
||||||
|
import { sluggify } from "@blitzkit/core";
|
||||||
|
import { literalsArray } from "@blitzkit/i18n";
|
||||||
|
import { api } from "../core/api/dynamic";
|
||||||
import { Link } from "./Link";
|
import { Link } from "./Link";
|
||||||
import { Text } from "./Text";
|
import { Text } from "./Text";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
value: string;
|
value: string;
|
||||||
|
locale: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const bodyPattern = /^([a-z0-9-\/\.]+) (.+)$/;
|
const bodyPattern = /^([a-z0-9-\/\.]+) (.+)$/;
|
||||||
const { value } = Astro.props;
|
|
||||||
|
const { value, locale } = Astro.props;
|
||||||
|
const strings = await api.strings(locale);
|
||||||
|
|
||||||
const match = value.match(bodyPattern);
|
const match = value.match(bodyPattern);
|
||||||
|
|
||||||
if (match === null) {
|
if (match === null) {
|
||||||
@@ -18,4 +25,11 @@ const path = match[1];
|
|||||||
const message = match[2];
|
const message = match[2];
|
||||||
---
|
---
|
||||||
|
|
||||||
<Text><Text color="tomato">See</Text> <Link>{path}</Link> {message}</Text>
|
<Text>
|
||||||
|
{
|
||||||
|
literalsArray(strings.api.macros.see, {
|
||||||
|
path: <Link href={`#${sluggify(path)}`}>{path}</Link>,
|
||||||
|
message,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
</Text>
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { sluggify } from "@blitzkit/core";
|
||||||
import { PlayIcon } from "@radix-ui/react-icons";
|
import { PlayIcon } from "@radix-ui/react-icons";
|
||||||
import { Fragment, useState } from "react";
|
import { Fragment, useState } from "react";
|
||||||
import {
|
import {
|
||||||
@@ -40,7 +41,7 @@ function Content({ path }: Props) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.tester}>
|
<div className={styles.tester} id={sluggify(path)}>
|
||||||
<Text weight="medium">
|
<Text weight="medium">
|
||||||
<Code className={styles.title}>
|
<Code className={styles.title}>
|
||||||
{path.split(slugPattern).map((text, index) => {
|
{path.split(slugPattern).map((text, index) => {
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
.link {
|
.link {
|
||||||
text-decoration-color: currentColor;
|
text-decoration-color: currentColor;
|
||||||
|
|
||||||
display: flex;
|
display: inline;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: var(--space-3);
|
gap: var(--space-3);
|
||||||
|
|
||||||
|
|||||||
File renamed without changes.
@@ -1,6 +1,6 @@
|
|||||||
import type { ComponentProps } from "react";
|
import type { ComponentProps } from "react";
|
||||||
import { classNames } from "../../core/ui/classNames";
|
import { classNames } from "../../core/ui/classNames";
|
||||||
import "./index.css";
|
import styles from "./index.module.css";
|
||||||
|
|
||||||
export interface TextProps extends ComponentProps<"span"> {
|
export interface TextProps extends ComponentProps<"span"> {
|
||||||
weight?: "light" | "regular" | "medium" | "bold" | "black";
|
weight?: "light" | "regular" | "medium" | "bold" | "black";
|
||||||
@@ -20,7 +20,7 @@ export function Text({
|
|||||||
}: TextProps) {
|
}: TextProps) {
|
||||||
return (
|
return (
|
||||||
<span
|
<span
|
||||||
className={classNames("text", className)}
|
className={classNames(styles.text, className)}
|
||||||
data-low-contrast={lowContrast}
|
data-low-contrast={lowContrast}
|
||||||
data-size={size}
|
data-size={size}
|
||||||
data-weight={weight}
|
data-weight={weight}
|
||||||
|
|||||||
Reference in new issue
Block a user