@writedocs/generator 0.10.10 → 0.10.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/components/Accordion.astro +5 -3
- package/src/components/Callout.astro +8 -3
- package/src/components/Card.astro +4 -1
- package/src/components/Check.astro +1 -1
- package/src/components/Danger.astro +1 -1
- package/src/components/Info.astro +1 -1
- package/src/components/Note.astro +1 -1
- package/src/components/Tip.astro +1 -1
- package/src/components/Update.astro +4 -3
- package/src/components/Warning.astro +1 -1
- package/src/lib/link-check.js +10 -1
- package/src/lib/mdx-title-anchor-ids.js +54 -9
- package/src/lib/plain-text.js +32 -0
package/package.json
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
---
|
|
2
2
|
import { extraClasses } from './class-names';
|
|
3
3
|
import AppIcon from "./AppIcon.astro";
|
|
4
|
+
import { plainText } from "../lib/plain-text.js";
|
|
4
5
|
|
|
5
6
|
interface Props {
|
|
6
|
-
title
|
|
7
|
+
// A string, or JSX - `title={<><code>X</code> details</>}`, as Mintlify allows.
|
|
8
|
+
title: string | object;
|
|
7
9
|
icon?: string;
|
|
8
10
|
// Mintlify parity: `description` is a muted second line under the title,
|
|
9
11
|
// `defaultOpen` renders the accordion already expanded.
|
|
@@ -30,7 +32,7 @@ function slugify(value: string): string {
|
|
|
30
32
|
.replace(/[^a-z0-9]+/g, "-")
|
|
31
33
|
.replace(/^-+|-+$/g, "");
|
|
32
34
|
}
|
|
33
|
-
const titleId = _titleId ?? slugify(title);
|
|
35
|
+
const titleId = _titleId ?? slugify(plainText(title));
|
|
34
36
|
---
|
|
35
37
|
|
|
36
38
|
<details class:list={["wd-accordion", extraClasses(Astro.props)]} open={defaultOpen}>
|
|
@@ -39,7 +41,7 @@ const titleId = _titleId ?? slugify(title);
|
|
|
39
41
|
{icon && <AppIcon icon={icon} class="wd-accordion-icon" />}
|
|
40
42
|
<span class="wd-accordion-text">
|
|
41
43
|
<span class="wd-accordion-title" id={titleId}>
|
|
42
|
-
{title}
|
|
44
|
+
{Astro.slots.has("wd-title") ? <slot name="wd-title" /> : title}
|
|
43
45
|
<a class="wd-heading-anchor" href={`#${titleId}`} aria-label="Link to this section"> # </a>
|
|
44
46
|
</span>
|
|
45
47
|
{description && <span class="wd-accordion-description">{description}</span>}
|
|
@@ -6,10 +6,11 @@ import { extraClasses } from './class-names';
|
|
|
6
6
|
// shorthand `<Info>` and get an identical result. Both forms take the
|
|
7
7
|
// same `title` prop.
|
|
8
8
|
import AppIcon from "./AppIcon.astro";
|
|
9
|
+
import { plainText } from "../lib/plain-text.js";
|
|
9
10
|
|
|
10
11
|
interface Props {
|
|
11
12
|
type?: "note" | "tip" | "warning" | "danger" | "info" | "check";
|
|
12
|
-
title?: string;
|
|
13
|
+
title?: string | object;
|
|
13
14
|
// Set automatically by remarkCalloutAnchorIds (src/lib/mdx-callout-anchor-ids.js)
|
|
14
15
|
// for every <Callout title="...">/<Note title="...">/etc. it finds in an
|
|
15
16
|
// .mdx file's own JSX, already deduped (-2, -3, ...) against every other
|
|
@@ -27,6 +28,10 @@ interface Props {
|
|
|
27
28
|
color?: string;
|
|
28
29
|
}
|
|
29
30
|
const { type = "note", title, _titleId, icon, color } = Astro.props as Props;
|
|
31
|
+
// A JSX title, moved into the `wd-title` slot by lib/mdx-title-anchor-ids.js.
|
|
32
|
+
// Checked by its content: the Note/Info/... wrappers forward the slot even
|
|
33
|
+
// when nothing fills it.
|
|
34
|
+
const titleHtml = Astro.slots.has("wd-title") ? (await Astro.slots.render("wd-title")).trim() : "";
|
|
30
35
|
const accentStyle = color ? `--wd-callout-accent: ${color}` : undefined;
|
|
31
36
|
// FontAwesome Solid (fa6-solid, already an installed @iconify-json/*
|
|
32
37
|
// collection - see astro.config.mjs's own comment on icon() needing no
|
|
@@ -49,7 +54,7 @@ function slugify(value: string): string {
|
|
|
49
54
|
.replace(/[^a-z0-9]+/g, "-")
|
|
50
55
|
.replace(/^-+|-+$/g, "");
|
|
51
56
|
}
|
|
52
|
-
const titleId = title ? (_titleId ?? slugify(title)) : undefined;
|
|
57
|
+
const titleId = title ? (_titleId ?? slugify(plainText(title))) : undefined;
|
|
53
58
|
---
|
|
54
59
|
|
|
55
60
|
<div class:list={["wd-callout", `wd-callout-${type}`, { "wd-callout-titled": Boolean(title) }, extraClasses(Astro.props)]} style={accentStyle}>
|
|
@@ -58,7 +63,7 @@ const titleId = title ? (_titleId ?? slugify(title)) : undefined;
|
|
|
58
63
|
{
|
|
59
64
|
title && (
|
|
60
65
|
<div class="wd-callout-title" id={titleId}>
|
|
61
|
-
{title}
|
|
66
|
+
{titleHtml ? <Fragment set:html={titleHtml} /> : title}
|
|
62
67
|
<a class="wd-heading-anchor" href={`#${titleId}`} aria-label="Link to this section">
|
|
63
68
|
#
|
|
64
69
|
</a>
|
|
@@ -38,6 +38,9 @@ interface Props {
|
|
|
38
38
|
}
|
|
39
39
|
const { title, icon, img, method, href, color, horizontal = false, cta, arrow = false } = Astro.props as Props;
|
|
40
40
|
const iconStyle = color ? `color: ${color}` : undefined;
|
|
41
|
+
// An icon written as markup (an inline <svg>), moved into the `wd-icon` slot
|
|
42
|
+
// by lib/mdx-title-anchor-ids.js.
|
|
43
|
+
const iconHtml = Astro.slots.has('wd-icon') ? (await Astro.slots.render('wd-icon')).trim() : '';
|
|
41
44
|
const Tag = href ? 'a' : 'div';
|
|
42
45
|
const KNOWN_METHODS = ['get', 'post', 'put', 'patch', 'delete'];
|
|
43
46
|
const methodLower = method?.toLowerCase();
|
|
@@ -45,7 +48,7 @@ const methodClass = methodLower && KNOWN_METHODS.includes(methodLower) ? methodL
|
|
|
45
48
|
---
|
|
46
49
|
<Tag class:list={['wd-card', { 'wd-card-horizontal': horizontal, 'wd-card-has-arrow': arrow && href }, extraClasses(Astro.props)]} href={href}>
|
|
47
50
|
{img && <img src={img} alt="" class="wd-card-image" />}
|
|
48
|
-
{!img && <AppIcon icon={icon} class="wd-card-icon" style={iconStyle} />}
|
|
51
|
+
{!img && (iconHtml ? <span class="wd-card-icon" style={iconStyle} set:html={iconHtml} /> : <AppIcon icon={icon} class="wd-card-icon" style={iconStyle} />)}
|
|
49
52
|
<div class="wd-card-text">
|
|
50
53
|
<div class="wd-card-title-row">
|
|
51
54
|
<h3 class="wd-card-title">{title}</h3>
|
|
@@ -10,4 +10,4 @@ interface Props {
|
|
|
10
10
|
}
|
|
11
11
|
const { title, _titleId } = Astro.props as Props;
|
|
12
12
|
---
|
|
13
|
-
<Callout type="check" title={title} _titleId={_titleId} class={Astro.props.class} className={Astro.props.className}><slot /></Callout>
|
|
13
|
+
<Callout type="check" title={title} _titleId={_titleId} class={Astro.props.class} className={Astro.props.className}><slot name="wd-title" slot="wd-title" /><slot /></Callout>
|
|
@@ -9,4 +9,4 @@ interface Props {
|
|
|
9
9
|
}
|
|
10
10
|
const { title, _titleId } = Astro.props as Props;
|
|
11
11
|
---
|
|
12
|
-
<Callout type="danger" title={title} _titleId={_titleId} class={Astro.props.class} className={Astro.props.className}><slot /></Callout>
|
|
12
|
+
<Callout type="danger" title={title} _titleId={_titleId} class={Astro.props.class} className={Astro.props.className}><slot name="wd-title" slot="wd-title" /><slot /></Callout>
|
|
@@ -9,4 +9,4 @@ interface Props {
|
|
|
9
9
|
}
|
|
10
10
|
const { title, _titleId } = Astro.props as Props;
|
|
11
11
|
---
|
|
12
|
-
<Callout type="info" title={title} _titleId={_titleId} class={Astro.props.class} className={Astro.props.className}><slot /></Callout>
|
|
12
|
+
<Callout type="info" title={title} _titleId={_titleId} class={Astro.props.class} className={Astro.props.className}><slot name="wd-title" slot="wd-title" /><slot /></Callout>
|
|
@@ -9,4 +9,4 @@ interface Props {
|
|
|
9
9
|
}
|
|
10
10
|
const { title, _titleId } = Astro.props as Props;
|
|
11
11
|
---
|
|
12
|
-
<Callout type="note" title={title} _titleId={_titleId} class={Astro.props.class} className={Astro.props.className}><slot /></Callout>
|
|
12
|
+
<Callout type="note" title={title} _titleId={_titleId} class={Astro.props.class} className={Astro.props.className}><slot name="wd-title" slot="wd-title" /><slot /></Callout>
|
package/src/components/Tip.astro
CHANGED
|
@@ -9,4 +9,4 @@ interface Props {
|
|
|
9
9
|
}
|
|
10
10
|
const { title, _titleId } = Astro.props as Props;
|
|
11
11
|
---
|
|
12
|
-
<Callout type="tip" title={title} _titleId={_titleId} class={Astro.props.class} className={Astro.props.className}><slot /></Callout>
|
|
12
|
+
<Callout type="tip" title={title} _titleId={_titleId} class={Astro.props.class} className={Astro.props.className}><slot name="wd-title" slot="wd-title" /><slot /></Callout>
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
import { extraClasses } from './class-names';
|
|
3
|
+
import { plainText } from '../lib/plain-text.js';
|
|
3
4
|
// Mintlify's <Update> - one changelog entry: `label` (usually a date or
|
|
4
5
|
// version) in a left column that sticks while its content scrolls, with
|
|
5
6
|
// `description` and `tags` under it. The label is a linkable anchor, deduped
|
|
@@ -11,7 +12,7 @@ import { extraClasses } from './class-names';
|
|
|
11
12
|
// in its right-hand panel. `rss` is accepted but unused: writedocs doesn't
|
|
12
13
|
// generate an RSS feed from updates.
|
|
13
14
|
interface Props {
|
|
14
|
-
label: string;
|
|
15
|
+
label: string | object;
|
|
15
16
|
description?: string;
|
|
16
17
|
tags?: string[];
|
|
17
18
|
rss?: { title?: string; description?: string };
|
|
@@ -24,12 +25,12 @@ function slugify(value: string): string {
|
|
|
24
25
|
.replace(/[^a-z0-9]+/g, '-')
|
|
25
26
|
.replace(/^-+|-+$/g, '');
|
|
26
27
|
}
|
|
27
|
-
const id = _titleId ?? slugify(label);
|
|
28
|
+
const id = _titleId ?? slugify(plainText(label));
|
|
28
29
|
---
|
|
29
30
|
<section class:list={["wd-update", extraClasses(Astro.props)]} data-update-tags={JSON.stringify(tags)}>
|
|
30
31
|
<div class="wd-update-meta">
|
|
31
32
|
<div class="wd-update-label" id={id}>
|
|
32
|
-
<a href={`#${id}`}>{label}</a>
|
|
33
|
+
<a href={`#${id}`}>{Astro.slots.has('wd-title') ? <slot name="wd-title" /> : label}</a>
|
|
33
34
|
</div>
|
|
34
35
|
{description && <div class="wd-update-description">{description}</div>}
|
|
35
36
|
{tags.length > 0 && (
|
|
@@ -9,4 +9,4 @@ interface Props {
|
|
|
9
9
|
}
|
|
10
10
|
const { title, _titleId } = Astro.props as Props;
|
|
11
11
|
---
|
|
12
|
-
<Callout type="warning" title={title} _titleId={_titleId} class={Astro.props.class} className={Astro.props.className}><slot /></Callout>
|
|
12
|
+
<Callout type="warning" title={title} _titleId={_titleId} class={Astro.props.class} className={Astro.props.className}><slot name="wd-title" slot="wd-title" /><slot /></Callout>
|
package/src/lib/link-check.js
CHANGED
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
//
|
|
28
28
|
// External links (http:, mailto:, ...) aren't checked.
|
|
29
29
|
import fs from 'node:fs';
|
|
30
|
+
import { estreeText } from './plain-text.js';
|
|
30
31
|
import path from 'node:path';
|
|
31
32
|
import { createRequire } from 'node:module';
|
|
32
33
|
import { pathToFileURL } from 'node:url';
|
|
@@ -108,6 +109,14 @@ function attribute(node, name) {
|
|
|
108
109
|
return attr && typeof attr.value === 'string' ? attr.value : null;
|
|
109
110
|
}
|
|
110
111
|
|
|
112
|
+
/** A component's title as text - a JSX title (`title={<><code>X</code> y</>}`)
|
|
113
|
+
* included, the way lib/mdx-title-anchor-ids.js reads it. */
|
|
114
|
+
function titleText(node, name) {
|
|
115
|
+
const attr = node.attributes?.find((a) => a.type === 'mdxJsxAttribute' && a.name === name);
|
|
116
|
+
if (!attr) return null;
|
|
117
|
+
return typeof attr.value === 'string' ? attr.value : estreeText(attr.value?.data?.estree) || null;
|
|
118
|
+
}
|
|
119
|
+
|
|
111
120
|
/** Everything a page or snippet file links to and can be linked to. */
|
|
112
121
|
async function scanFile(absPath, { slug, Slugger }) {
|
|
113
122
|
let raw;
|
|
@@ -175,7 +184,7 @@ async function scanFile(absPath, { slug, Slugger }) {
|
|
|
175
184
|
const id = attribute(node, 'id');
|
|
176
185
|
if (id) anchors.add(id);
|
|
177
186
|
if (TITLED_COMPONENTS.includes(node.name)) {
|
|
178
|
-
const title =
|
|
187
|
+
const title = titleText(node, node.name === 'Update' ? 'label' : 'title');
|
|
179
188
|
const base = title ? titleSlug(title) : '';
|
|
180
189
|
if (base) {
|
|
181
190
|
const prior = titleSeen.get(base) ?? 0;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { visit } from 'unist-util-visit';
|
|
2
|
+
import { estreeText } from './plain-text.js';
|
|
2
3
|
|
|
3
4
|
// Every JSX tag name that can carry a `title` prop and render a linkable
|
|
4
5
|
// heading of its own - the 6 Callout variants remarkInjectBuiltinComponents
|
|
@@ -52,12 +53,12 @@ function slugify(value) {
|
|
|
52
53
|
* github-slugger itself runs once per document rather than being
|
|
53
54
|
* reimplemented per-heading.
|
|
54
55
|
*
|
|
55
|
-
*
|
|
56
|
-
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
56
|
+
* A literal `title="..."` comes as a plain string; `title={expr}` as an
|
|
57
|
+
* mdxJsxAttributeValueExpression, whose estree gives the text of a JSX title
|
|
58
|
+
* (`{<><code>SUCCEEDED</code> status details</>}` -> "SUCCEEDED status
|
|
59
|
+
* details", estreeText() in plain-text.js). An expression with no text of
|
|
60
|
+
* its own (a variable) gets no id here, and the component falls back to
|
|
61
|
+
* its own un-deduped slugify(plainText(title)).
|
|
61
62
|
*
|
|
62
63
|
* Scoped per MDX *file*, not per rendered page - a snippet imported into a
|
|
63
64
|
* page (see docs/dev/docs/snippets.mdx) is its own independently-compiled
|
|
@@ -66,18 +67,62 @@ function slugify(value) {
|
|
|
66
67
|
* remarkInjectBuiltinComponents' own per-file scope is: there's no single
|
|
67
68
|
* shared AST spanning both by the time either one's transform runs.
|
|
68
69
|
*/
|
|
70
|
+
/** Whether an estree program holds JSX - a title written as markup, not a
|
|
71
|
+
* plain expression like a variable. */
|
|
72
|
+
function hasJsx(node) {
|
|
73
|
+
if (!node || typeof node !== 'object') return false;
|
|
74
|
+
if (Array.isArray(node)) return node.some(hasJsx);
|
|
75
|
+
if (node.type === 'JSXElement' || node.type === 'JSXFragment') return true;
|
|
76
|
+
return Object.entries(node).some(([key, child]) => key !== 'loc' && key !== 'range' && child && typeof child === 'object' && hasJsx(child));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** A `<span slot="...">` child holding an attribute's JSX expression. */
|
|
80
|
+
function slotChild(slot, expression) {
|
|
81
|
+
return {
|
|
82
|
+
type: 'mdxJsxFlowElement',
|
|
83
|
+
name: 'span',
|
|
84
|
+
attributes: [{ type: 'mdxJsxAttribute', name: 'slot', value: slot }],
|
|
85
|
+
children: [{ type: 'mdxFlowExpression', value: expression.value, data: { estree: expression.data.estree } }],
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
69
89
|
export function remarkTitleAnchorIds() {
|
|
70
90
|
return (tree) => {
|
|
71
91
|
const seen = new Map();
|
|
72
92
|
visit(tree, ['mdxJsxFlowElement', 'mdxJsxTextElement'], (node) => {
|
|
93
|
+
// A Card's icon written as markup - an inline `<svg>` - moves into a
|
|
94
|
+
// `wd-icon` slot the same way a JSX title does (below), since an
|
|
95
|
+
// .astro component can't print JSX passed as a prop.
|
|
96
|
+
if (node.name === 'Card') {
|
|
97
|
+
const iconAttr = node.attributes?.find((attr) => attr.type === 'mdxJsxAttribute' && attr.name === 'icon');
|
|
98
|
+
const iconTree = iconAttr && typeof iconAttr.value !== 'string' ? iconAttr.value?.data?.estree : null;
|
|
99
|
+
if (iconTree && hasJsx(iconTree)) {
|
|
100
|
+
node.children.unshift(slotChild('wd-icon', iconAttr.value));
|
|
101
|
+
node.attributes = node.attributes.filter((attr) => attr !== iconAttr);
|
|
102
|
+
}
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
73
105
|
if (!node.name || !TITLED_COMPONENT_NAMES.includes(node.name)) return;
|
|
74
106
|
const attrName = ANCHOR_ATTRIBUTE[node.name] ?? 'title';
|
|
75
107
|
const titleAttr = node.attributes?.find(
|
|
76
108
|
(attr) => attr.type === 'mdxJsxAttribute' && attr.name === attrName
|
|
77
109
|
);
|
|
78
|
-
if (!titleAttr
|
|
79
|
-
|
|
80
|
-
|
|
110
|
+
if (!titleAttr) return;
|
|
111
|
+
// A JSX title - `title={<><code>SUCCEEDED</code> status details</>}` -
|
|
112
|
+
// gets its id from its text, like a plain one.
|
|
113
|
+
const estree = typeof titleAttr.value === 'string' ? null : titleAttr.value?.data?.estree;
|
|
114
|
+
const text = estree ? estreeText(estree) : typeof titleAttr.value === 'string' ? titleAttr.value : '';
|
|
115
|
+
// An .astro component can't print JSX passed as a prop ("[object
|
|
116
|
+
// Object]"), but it renders a named slot: the JSX moves into a
|
|
117
|
+
// `<span slot="wd-title">` child (the component shows that slot in
|
|
118
|
+
// place of the prop), and the prop becomes its text - for the
|
|
119
|
+
// component's own checks. Not a slot named `title`: Astro drops a
|
|
120
|
+
// prop that has the same name as a slot.
|
|
121
|
+
if (estree && hasJsx(estree)) {
|
|
122
|
+
node.children.unshift(slotChild('wd-title', titleAttr.value));
|
|
123
|
+
titleAttr.value = text;
|
|
124
|
+
}
|
|
125
|
+
const base = slugify(text);
|
|
81
126
|
if (!base) return;
|
|
82
127
|
const priorCount = seen.get(base) ?? 0;
|
|
83
128
|
seen.set(base, priorCount + 1);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// The text of a component prop that may be JSX rather than a string -
|
|
2
|
+
// Mintlify allows `title={<><code>SUCCEEDED</code> status details</>}` on an
|
|
3
|
+
// Accordion or a Callout. Used to make an anchor id from such a title.
|
|
4
|
+
// Plain JavaScript, so scripts/plain-text.test.js can check it.
|
|
5
|
+
|
|
6
|
+
/** The text of a rendered prop: a string, a number, an array of them, or a
|
|
7
|
+
* JSX element (its `props.children`, however deep). '' for anything else. */
|
|
8
|
+
export function plainText(value) {
|
|
9
|
+
if (value === null || value === undefined || typeof value === 'boolean') return '';
|
|
10
|
+
if (typeof value === 'string' || typeof value === 'number') return String(value);
|
|
11
|
+
if (Array.isArray(value)) return value.map(plainText).join('');
|
|
12
|
+
if (typeof value === 'object' && value.props) return plainText(value.props.children);
|
|
13
|
+
return '';
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The text of a JSX expression in MDX's syntax tree (an estree program, from
|
|
17
|
+
* an attribute like `title={<>...</>}`): its JSX text and string literals,
|
|
18
|
+
* in order. */
|
|
19
|
+
export function estreeText(node) {
|
|
20
|
+
if (!node || typeof node !== 'object') return '';
|
|
21
|
+
if (Array.isArray(node)) return node.map(estreeText).join('');
|
|
22
|
+
if (node.type === 'JSXText') return node.value;
|
|
23
|
+
if (node.type === 'Literal') return typeof node.value === 'string' || typeof node.value === 'number' ? String(node.value) : '';
|
|
24
|
+
if (node.type === 'TemplateLiteral') return node.quasis.map((q) => q.value.cooked ?? '').join('');
|
|
25
|
+
if (node.type === 'JSXAttribute') return '';
|
|
26
|
+
let text = '';
|
|
27
|
+
for (const [key, child] of Object.entries(node)) {
|
|
28
|
+
if (key === 'loc' || key === 'range' || key === 'position') continue;
|
|
29
|
+
if (child && typeof child === 'object') text += estreeText(child);
|
|
30
|
+
}
|
|
31
|
+
return text;
|
|
32
|
+
}
|