@sinnstack/ui 0.2.0 → 0.3.0
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/CHANGELOG.md +74 -0
- package/README.md +6 -4
- package/docs/blocks.md +373 -364
- package/docs/data.md +128 -128
- package/docs/finishes.md +165 -0
- package/docs/primitives.md +4 -4
- package/docs/seo.md +191 -187
- package/package.json +15 -24
- package/src/blocks/BlockRenderer.astro +13 -7
- package/src/blocks/CTABanner.astro +8 -8
- package/src/blocks/CTAPair.astro +9 -9
- package/src/blocks/CardGrid.astro +7 -7
- package/src/blocks/ConsentEmbed.astro +1 -1
- package/src/blocks/ConsentManager.astro +1 -1
- package/src/blocks/ContactCard.astro +14 -14
- package/src/blocks/ContactForm.astro +4 -4
- package/src/blocks/ErrorPage.astro +1 -1
- package/src/blocks/FAQ.astro +9 -9
- package/src/blocks/FeatureGrid.astro +7 -7
- package/src/blocks/Hero.astro +11 -11
- package/src/blocks/HeroCarousel.astro +11 -11
- package/src/blocks/HighlightCards.astro +10 -10
- package/src/blocks/LocationHours.astro +14 -14
- package/src/blocks/LogoCloud.astro +7 -7
- package/src/blocks/MaintenancePage.astro +8 -3
- package/src/blocks/MediaText.astro +12 -12
- package/src/blocks/PageHeader.astro +9 -9
- package/src/blocks/ProseSection.astro +8 -8
- package/src/blocks/Reviews.astro +11 -11
- package/src/blocks/RichText.astro +58 -57
- package/src/blocks/StatsBand.astro +117 -117
- package/src/blocks/Steps.astro +9 -9
- package/src/blocks/Testimonials.astro +13 -13
- package/src/blocks/cms/card-grid.ts +48 -80
- package/src/blocks/cms/consent.ts +1 -30
- package/src/blocks/cms/contact-card.ts +4 -46
- package/src/blocks/cms/contact-form.ts +2 -20
- package/src/blocks/cms/cta-banner.ts +2 -23
- package/src/blocks/cms/cta-pair.ts +4 -37
- package/src/blocks/cms/data-base.ts +2 -4
- package/src/blocks/cms/data.ts +19 -19
- package/src/blocks/cms/faq.ts +4 -36
- package/src/blocks/cms/feature-grid.ts +47 -80
- package/src/blocks/cms/fixtures.ts +591 -599
- package/src/blocks/cms/hero-carousel.ts +4 -48
- package/src/blocks/cms/hero.ts +2 -46
- package/src/blocks/cms/highlight-cards.ts +4 -39
- package/src/blocks/cms/images.ts +1 -2
- package/src/blocks/cms/index.ts +59 -87
- package/src/blocks/cms/location-hours.ts +3 -85
- package/src/blocks/cms/logo-cloud.ts +36 -62
- package/src/blocks/cms/media-text.ts +2 -20
- package/src/blocks/cms/page-header.ts +27 -49
- package/src/blocks/cms/pages.ts +35 -81
- package/src/blocks/cms/prose-section.ts +2 -22
- package/src/blocks/cms/reviews.ts +5 -56
- package/src/blocks/cms/schema-dsl.ts +134 -0
- package/src/blocks/cms/schema.ts +794 -0
- package/src/blocks/cms/shared.ts +63 -115
- package/src/blocks/cms/stats-band.ts +35 -61
- package/src/blocks/cms/steps.ts +4 -31
- package/src/blocks/cms/testimonials.ts +4 -38
- package/src/blocks/cms/topbar.ts +4 -41
- package/src/blocks/cms/types.ts +1 -37
- package/src/blocks/consent.ts +2 -2
- package/src/blocks/index.ts +2 -2
- package/src/blocks/internal/Actions.astro +11 -5
- package/src/blocks/internal/BlockHeader.astro +7 -7
- package/src/blocks/internal/Placeholder.astro +3 -3
- package/src/blocks/internal/fields.ts +18 -0
- package/src/blocks/internal/registry-contract.ts +67 -0
- package/src/blocks/internal/util.ts +4 -4
- package/src/blocks/lib/contact.ts +10 -4
- package/src/blocks/lib/faq.ts +1 -1
- package/src/blocks/lib/markdown.ts +350 -0
- package/src/blocks/lib/rich-text.ts +132 -143
- package/src/blocks/scripts/consent-manager.ts +1 -1
- package/src/blocks/scripts/consent-store.ts +1 -1
- package/src/blocks/scripts/hours.ts +3 -2
- package/src/blocks/types.ts +33 -23
- package/src/data/ChartContainer.astro +2 -1
- package/src/data/cms.ts +130 -190
- package/src/data/lib/chart.ts +13 -4
- package/src/data/schema.ts +65 -0
- package/src/finishes/aurora.css +75 -0
- package/src/finishes/autumn.css +87 -0
- package/src/finishes/blueprint.css +69 -0
- package/src/finishes/brutalist.css +55 -0
- package/src/finishes/clay.css +48 -0
- package/src/finishes/day-cycle.css +46 -0
- package/src/finishes/fog.css +71 -0
- package/src/finishes/glass.css +149 -0
- package/src/finishes/holographic.css +91 -0
- package/src/finishes/night.css +77 -0
- package/src/finishes/paper.css +58 -0
- package/src/finishes/polished-glass.css +159 -0
- package/src/finishes/rain.css +84 -0
- package/src/finishes/scripts/day-cycle.ts +30 -0
- package/src/finishes/scripts/holographic.ts +37 -0
- package/src/finishes/scripts/spotlight.ts +30 -0
- package/src/finishes/spotlight.css +41 -0
- package/src/finishes/spring.css +80 -0
- package/src/finishes/terminal.css +49 -0
- package/src/primitives/Divider.astro +1 -1
- package/src/primitives/FocusRing.astro +1 -1
- package/src/primitives/FormField.astro +2 -2
- package/src/primitives/Image.astro +3 -3
- package/src/primitives/Input.astro +4 -2
- package/src/primitives/MetricCard.astro +3 -1
- package/src/primitives/Picture.astro +3 -3
- package/src/primitives/Prose.astro +1 -1
- package/src/primitives/Select.astro +4 -2
- package/src/primitives/Slider.astro +1 -1
- package/src/primitives/Table.astro +1 -1
- package/src/primitives/Textarea.astro +4 -2
- package/src/primitives/Thumbnail.astro +2 -2
- package/src/primitives/index.ts +4 -4
- package/src/primitives/internal/ChoiceField.astro +4 -2
- package/src/primitives/scripts/slider.ts +1 -1
- package/src/primitives/scripts/table-sort.ts +2 -2
- package/src/seo/cms.ts +59 -80
- package/src/seo/index.ts +1 -1
- package/src/seo/integration.ts +40 -3
- package/src/seo/lib/jsonld.ts +10 -2
- package/src/seo/lib/meta.ts +2 -2
- package/src/seo/lib/redirects.ts +14 -4
- package/src/seo/lib/rss.ts +1 -1
- package/src/seo/lib/sitemap.ts +1 -1
- package/src/seo/schema.ts +43 -0
- package/src/blocks/internal/tina.ts +0 -19
- /package/src/blocks/{lib → internal}/consent.ts +0 -0
- /package/src/blocks/{lib → internal}/reviews.ts +0 -0
- /package/src/blocks/{lib → internal}/slug.ts +0 -0
- /package/src/data/{lib → internal}/scale.ts +0 -0
- /package/src/primitives/{lib → internal}/image-plan.ts +0 -0
- /package/src/primitives/{lib → internal}/image.ts +0 -0
- /package/src/primitives/{lib → internal}/slider.ts +0 -0
- /package/src/seo/{lib → internal}/url.ts +0 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @internal Type-only contract between each registry entry's mapper and its component: a mapper may
|
|
3
|
+
* only return props the component declares, with types it accepts. `astro check` fails otherwise.
|
|
4
|
+
* Nothing here runs; the file only exists for the type checker.
|
|
5
|
+
*/
|
|
6
|
+
import type { ComponentProps } from 'astro/types';
|
|
7
|
+
import type Hero from '../Hero.astro';
|
|
8
|
+
import type CTABanner from '../CTABanner.astro';
|
|
9
|
+
import type FeatureGrid from '../FeatureGrid.astro';
|
|
10
|
+
import type CardGrid from '../CardGrid.astro';
|
|
11
|
+
import type LogoCloud from '../LogoCloud.astro';
|
|
12
|
+
import type HeroCarousel from '../HeroCarousel.astro';
|
|
13
|
+
import type PageHeader from '../PageHeader.astro';
|
|
14
|
+
import type ContactCard from '../ContactCard.astro';
|
|
15
|
+
import type Steps from '../Steps.astro';
|
|
16
|
+
import type MediaText from '../MediaText.astro';
|
|
17
|
+
import type CTAPair from '../CTAPair.astro';
|
|
18
|
+
import type StatsBand from '../StatsBand.astro';
|
|
19
|
+
import type HighlightCards from '../HighlightCards.astro';
|
|
20
|
+
import type ContactForm from '../ContactForm.astro';
|
|
21
|
+
import type ProseSection from '../ProseSection.astro';
|
|
22
|
+
import type FAQ from '../FAQ.astro';
|
|
23
|
+
import type Testimonials from '../Testimonials.astro';
|
|
24
|
+
import type Reviews from '../Reviews.astro';
|
|
25
|
+
import type LocationHours from '../LocationHours.astro';
|
|
26
|
+
import type { heroToProps } from '../cms/hero';
|
|
27
|
+
import type { ctaBannerToProps } from '../cms/cta-banner';
|
|
28
|
+
import type { featureGridToProps } from '../cms/feature-grid';
|
|
29
|
+
import type { cardGridToProps } from '../cms/card-grid';
|
|
30
|
+
import type { logoCloudToProps } from '../cms/logo-cloud';
|
|
31
|
+
import type { heroCarouselToProps } from '../cms/hero-carousel';
|
|
32
|
+
import type { pageHeaderToProps } from '../cms/page-header';
|
|
33
|
+
import type { contactCardToProps } from '../cms/contact-card';
|
|
34
|
+
import type { stepsToProps } from '../cms/steps';
|
|
35
|
+
import type { mediaTextToProps } from '../cms/media-text';
|
|
36
|
+
import type { ctaPairToProps } from '../cms/cta-pair';
|
|
37
|
+
import type { statsBandToProps } from '../cms/stats-band';
|
|
38
|
+
import type { highlightCardsToProps } from '../cms/highlight-cards';
|
|
39
|
+
import type { contactFormToProps } from '../cms/contact-form';
|
|
40
|
+
import type { proseSectionToProps } from '../cms/prose-section';
|
|
41
|
+
import type { faqToProps } from '../cms/faq';
|
|
42
|
+
import type { testimonialsToProps } from '../cms/testimonials';
|
|
43
|
+
import type { reviewsToProps } from '../cms/reviews';
|
|
44
|
+
import type { locationHoursToProps } from '../cms/location-hours';
|
|
45
|
+
type Fits<F extends (...a: any) => any, C extends (p: any) => any> = [Exclude<keyof ReturnType<F>, keyof ComponentProps<C>>] extends [never] ? (ReturnType<F> extends Partial<ComponentProps<C>> ? true : false) : false;
|
|
46
|
+
export type RegistryContract = [
|
|
47
|
+
Fits<typeof heroToProps, typeof Hero>, // hero
|
|
48
|
+
Fits<typeof ctaBannerToProps, typeof CTABanner>, // ctaBanner
|
|
49
|
+
Fits<typeof featureGridToProps, typeof FeatureGrid>, // featureGrid
|
|
50
|
+
Fits<typeof cardGridToProps, typeof CardGrid>, // cardGrid
|
|
51
|
+
Fits<typeof logoCloudToProps, typeof LogoCloud>, // logoCloud
|
|
52
|
+
Fits<typeof heroCarouselToProps, typeof HeroCarousel>, // heroCarousel
|
|
53
|
+
Fits<typeof pageHeaderToProps, typeof PageHeader>, // pageHeader
|
|
54
|
+
Fits<typeof contactCardToProps, typeof ContactCard>, // contactCard
|
|
55
|
+
Fits<typeof stepsToProps, typeof Steps>, // steps
|
|
56
|
+
Fits<typeof mediaTextToProps, typeof MediaText>, // mediaText
|
|
57
|
+
Fits<typeof ctaPairToProps, typeof CTAPair>, // ctaPair
|
|
58
|
+
Fits<typeof statsBandToProps, typeof StatsBand>, // statsBand
|
|
59
|
+
Fits<typeof highlightCardsToProps, typeof HighlightCards>, // highlightCards
|
|
60
|
+
Fits<typeof contactFormToProps, typeof ContactForm>, // contactForm
|
|
61
|
+
Fits<typeof proseSectionToProps, typeof ProseSection>, // proseSection
|
|
62
|
+
Fits<typeof faqToProps, typeof FAQ>, // faq
|
|
63
|
+
Fits<typeof testimonialsToProps, typeof Testimonials>, // testimonials
|
|
64
|
+
Fits<typeof reviewsToProps, typeof Reviews>, // reviews
|
|
65
|
+
Fits<typeof locationHoursToProps, typeof LocationHours>, // locationHours
|
|
66
|
+
];
|
|
67
|
+
export const registryContract: RegistryContract = [true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true];
|
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
import { uid } from '../../primitives/utils';
|
|
3
3
|
import type { ActionItem } from '../types';
|
|
4
4
|
|
|
5
|
-
export { slugify } from '
|
|
6
|
-
import { slugify } from '
|
|
5
|
+
export { slugify } from './slug';
|
|
6
|
+
import { slugify } from './slug';
|
|
7
7
|
|
|
8
8
|
/** The section's id (from `anchor` or `id`) and a heading id derived from it. */
|
|
9
|
-
export function blockIds(name: string, anchor?: string, id?: string): { sectionId?: string; headingId: string } {
|
|
10
|
-
const sectionId = anchor ? slugify(anchor) || undefined : id;
|
|
9
|
+
export function blockIds(name: string, anchor?: string, id?: string | null): { sectionId?: string; headingId: string } {
|
|
10
|
+
const sectionId = anchor ? slugify(anchor) || undefined : (id ?? undefined);
|
|
11
11
|
return { sectionId, headingId: `${sectionId ?? uid(`sinn-${name}`)}-title` };
|
|
12
12
|
}
|
|
13
13
|
|
|
@@ -45,11 +45,17 @@ export function mailHref(value: string): string | undefined {
|
|
|
45
45
|
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v) ? `mailto:${v}` : undefined;
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
/**
|
|
49
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Only these schemes become links (CMS content can't inject javascript: or data: URLs).
|
|
50
|
+
* Browsers drop tabs, newlines and leading control characters from URLs (`java<TAB>script:` runs as
|
|
51
|
+
* `javascript:`) and read `\` as `/`, so values containing them are refused outright, as are
|
|
52
|
+
* protocol-relative `//host` links (they go to another site; write `https://` instead).
|
|
53
|
+
*/
|
|
54
|
+
export function safeHref(href: string | null | undefined): string | undefined {
|
|
50
55
|
const v = typeof href === 'string' ? href.trim() : '';
|
|
51
|
-
if (!v) return undefined;
|
|
52
|
-
|
|
56
|
+
if (!v || /[\x00-\x1f\x7f\\]/.test(v) || v.startsWith('//')) return undefined;
|
|
57
|
+
// A scheme needs something after it: bare `https://`, `mailto:` or `tel:` links nowhere.
|
|
58
|
+
if (/^(https?:\/\/[^/\s]|(mailto|tel|sms):\S)/i.test(v) || v.startsWith('/') || v.startsWith('#') || v.startsWith('?')) return v;
|
|
53
59
|
if (/^[a-z][a-z0-9+.-]*:/i.test(v)) return undefined; // any other scheme
|
|
54
60
|
return v; // relative path
|
|
55
61
|
}
|
package/src/blocks/lib/faq.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** FAQ helpers: item normalization (ids, empty answers) and structured-data input. */
|
|
2
2
|
import type { FaqItem } from '../types';
|
|
3
|
-
import { slugify } from '
|
|
3
|
+
import { slugify } from '../internal/slug';
|
|
4
4
|
import { isRichTextEmpty, richTextPlain } from './rich-text';
|
|
5
5
|
|
|
6
6
|
export interface NormalizedFaq extends FaqItem {
|
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown ⇄ rich-text tree, for CMS rich text stored as Markdown strings.
|
|
3
|
+
*
|
|
4
|
+
* `markdownToRichText` parses the Markdown editors write into the node tree RichText renders, so
|
|
5
|
+
* the renderer's allowlist still decides what reaches the page: raw HTML stays literal text (Astro
|
|
6
|
+
* escapes it) and link/image URLs still pass safeLinkUrl / safeMediaUrl.
|
|
7
|
+
*
|
|
8
|
+
* Supported: paragraphs (a single newline is a line break), `#` headings, `-`/`*`/`+` and `1.`
|
|
9
|
+
* lists (nested by indenting), `>` quotes, fenced code, `---` rules, pipe tables, an image on a
|
|
10
|
+
* line of its own (``), and inline **bold**, *italic*,
|
|
11
|
+
* ~~strikethrough~~, `code`, [links](/path "title") and <https://autolinks>. Backslash escapes
|
|
12
|
+
* any of these characters.
|
|
13
|
+
*
|
|
14
|
+
* `richTextToMarkdown` goes the other way, for moving rich-text trees (e.g. from TinaCMS) into
|
|
15
|
+
* Markdown content.
|
|
16
|
+
*/
|
|
17
|
+
import type { RichTextNode, RichTextRoot } from './rich-text';
|
|
18
|
+
|
|
19
|
+
type Marks = Pick<RichTextNode, 'bold' | 'italic' | 'strikethrough' | 'code'>;
|
|
20
|
+
|
|
21
|
+
// --- Inline --------------------------------------------------------------------------------------
|
|
22
|
+
|
|
23
|
+
const PUNCT = /[\\`*_{}[\]()#+\-.!|~<>"']/;
|
|
24
|
+
const isWord = (c: string | undefined) => Boolean(c && /[\p{L}\p{N}]/u.test(c));
|
|
25
|
+
|
|
26
|
+
/** Index of the closing delimiter `d` for an opener at `from`, or -1. Skips code spans and escapes. */
|
|
27
|
+
function closing(s: string, from: number, d: string): number {
|
|
28
|
+
for (let i = from; i < s.length; i++) {
|
|
29
|
+
const c = s[i];
|
|
30
|
+
if (c === '\\') { i++; continue; }
|
|
31
|
+
if (c === '`') {
|
|
32
|
+
const run = /^`+/.exec(s.slice(i))![0];
|
|
33
|
+
const end = s.indexOf(run, i + run.length);
|
|
34
|
+
if (end > 0) { i = end + run.length - 1; continue; }
|
|
35
|
+
}
|
|
36
|
+
if (s.startsWith(d, i) && i > from && !/\s/.test(s[i - 1])) {
|
|
37
|
+
// A single * or _ must not be the start of a double one.
|
|
38
|
+
if (d.length === 1 && s[i + 1] === d) { i++; continue; }
|
|
39
|
+
if (d === '_' && isWord(s[i + 1])) continue;
|
|
40
|
+
// `***` after `**bold *both`: the inner `*` closes first, so take the last two.
|
|
41
|
+
if (d.length === 2 && d[0] !== '~') {
|
|
42
|
+
let end = i + 2;
|
|
43
|
+
while (s[end] === d[0]) end++;
|
|
44
|
+
return end - 2;
|
|
45
|
+
}
|
|
46
|
+
return i;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return -1;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** Index of the `]` that matches the `[` at `open`, or -1. */
|
|
53
|
+
function bracketEnd(s: string, open: number): number {
|
|
54
|
+
let depth = 0;
|
|
55
|
+
for (let i = open; i < s.length; i++) {
|
|
56
|
+
if (s[i] === '\\') { i++; continue; }
|
|
57
|
+
if (s[i] === '[') depth++;
|
|
58
|
+
if (s[i] === ']' && --depth === 0) return i;
|
|
59
|
+
}
|
|
60
|
+
return -1;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** `(url "title")` at `at`: the url, optional title and the index after `)`. */
|
|
64
|
+
function destination(s: string, at: number): { url: string; title?: string; end: number } | undefined {
|
|
65
|
+
if (s[at] !== '(') return undefined;
|
|
66
|
+
const m = /^\(\s*(<[^>\n]*>|[^\s()]*(?:\([^\s()]*\)[^\s()]*)*)(?:\s+"((?:[^"\\]|\\.)*)")?\s*\)/.exec(s.slice(at));
|
|
67
|
+
if (!m) return undefined;
|
|
68
|
+
const url = m[1].startsWith('<') ? m[1].slice(1, -1) : m[1];
|
|
69
|
+
return { url, title: m[2]?.replace(/\\(.)/g, '$1'), end: at + m[0].length };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function parseInline(s: string, marks: Marks = {}): RichTextNode[] {
|
|
73
|
+
const out: RichTextNode[] = [];
|
|
74
|
+
let buf = '';
|
|
75
|
+
const flush = () => {
|
|
76
|
+
if (buf) out.push({ type: 'text', text: buf, ...marks });
|
|
77
|
+
buf = '';
|
|
78
|
+
};
|
|
79
|
+
const wrap = (inner: RichTextNode[]) => { flush(); out.push(...inner); };
|
|
80
|
+
|
|
81
|
+
for (let i = 0; i < s.length; i++) {
|
|
82
|
+
const c = s[i];
|
|
83
|
+
if (c === '\\' && i + 1 < s.length && PUNCT.test(s[i + 1])) { buf += s[++i]; continue; }
|
|
84
|
+
if (c === '\n') {
|
|
85
|
+
buf = buf.replace(/[ \t]+$/, '');
|
|
86
|
+
flush();
|
|
87
|
+
out.push({ type: 'break' });
|
|
88
|
+
while (s[i + 1] === ' ' || s[i + 1] === '\t') i++;
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
91
|
+
if (c === '`') {
|
|
92
|
+
const run = /^`+/.exec(s.slice(i))![0];
|
|
93
|
+
const end = s.indexOf(run, i + run.length);
|
|
94
|
+
if (end > 0) {
|
|
95
|
+
flush();
|
|
96
|
+
let code = s.slice(i + run.length, end).replace(/\n/g, ' ');
|
|
97
|
+
if (/^ .*[^ ].* $/.test(code)) code = code.slice(1, -1);
|
|
98
|
+
out.push({ type: 'text', text: code, ...marks, code: true });
|
|
99
|
+
i = end + run.length - 1;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
buf += run;
|
|
103
|
+
i += run.length - 1;
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
if (c === '<') {
|
|
107
|
+
const m = /^<((?:https?:\/\/|mailto:)[^\s<>]+)>/i.exec(s.slice(i));
|
|
108
|
+
if (m) {
|
|
109
|
+
flush();
|
|
110
|
+
const label = m[1].replace(/^mailto:/i, '');
|
|
111
|
+
out.push({ type: 'a', url: m[1], children: [{ type: 'text', text: label, ...marks }] });
|
|
112
|
+
i += m[0].length - 1;
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (c === '!' && s[i + 1] === '[') {
|
|
117
|
+
// Images only render on a line of their own (a block); inline, keep the alt text.
|
|
118
|
+
const close = bracketEnd(s, i + 1);
|
|
119
|
+
const dest = close > 0 ? destination(s, close + 1) : undefined;
|
|
120
|
+
if (dest) { buf += s.slice(i + 2, close); i = dest.end - 1; continue; }
|
|
121
|
+
}
|
|
122
|
+
if (c === '[') {
|
|
123
|
+
const close = bracketEnd(s, i);
|
|
124
|
+
const dest = close > 0 ? destination(s, close + 1) : undefined;
|
|
125
|
+
if (dest) {
|
|
126
|
+
flush();
|
|
127
|
+
out.push({ type: 'a', url: dest.url, ...(dest.title ? { title: dest.title } : {}), children: parseInline(s.slice(i + 1, close), marks) });
|
|
128
|
+
i = dest.end - 1;
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const delim = s.startsWith('**', i) ? '**' : s.startsWith('__', i) ? '__' : s.startsWith('~~', i) ? '~~' : c === '*' || c === '_' ? c : undefined;
|
|
133
|
+
if (delim && !/\s/.test(s[i + delim.length] ?? ' ') && !(delim[0] === '_' && isWord(s[i - 1]))) {
|
|
134
|
+
const end = closing(s, i + delim.length, delim);
|
|
135
|
+
if (end > 0) {
|
|
136
|
+
const mark: Marks = delim === '~~' ? { strikethrough: true } : delim.length === 2 ? { bold: true } : { italic: true };
|
|
137
|
+
wrap(parseInline(s.slice(i + delim.length, end), { ...marks, ...mark }));
|
|
138
|
+
i = end + delim.length - 1;
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
buf += c;
|
|
143
|
+
}
|
|
144
|
+
flush();
|
|
145
|
+
return out;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// --- Blocks --------------------------------------------------------------------------------------
|
|
149
|
+
|
|
150
|
+
const FENCE = /^ {0,3}(`{3,}|~{3,})\s*([\w+#.-]*)[^\n]*$/;
|
|
151
|
+
const HEADING = /^ {0,3}(#{1,6})(?:[ \t]+(.*?))?(?:[ \t]+#+)?[ \t]*$/;
|
|
152
|
+
const HR = /^ {0,3}([-*_])(?:[ \t]*\1){2,}[ \t]*$/;
|
|
153
|
+
const QUOTE = /^ {0,3}> ?(.*)$/;
|
|
154
|
+
const ITEM = /^( *)([-*+]|\d{1,9}[.)])([ \t]+(.*))?$/;
|
|
155
|
+
const TABLE_RULE = /^\s*\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)*\|?\s*$/;
|
|
156
|
+
const IMAGE = /^\s*!\[((?:[^\]\\]|\\.)*)\]\(\s*(<[^>\n]*>|\S+?)(?:\s+"((?:[^"\\]|\\.)*)")?\s*\)\s*$/;
|
|
157
|
+
|
|
158
|
+
const startsBlock = (line: string) => FENCE.test(line) || HEADING.test(line) || HR.test(line) || QUOTE.test(line) || /^ {0,3}([-*+]|1[.)])[ \t]+\S/.test(line);
|
|
159
|
+
|
|
160
|
+
function cells(row: string): string[] {
|
|
161
|
+
const t = row.trim().replace(/^\|/, '').replace(/(?<!\\)\|$/, '');
|
|
162
|
+
return t.split(/(?<!\\)\|/).map((c) => c.trim().replace(/\\\|/g, '|'));
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function parseBlocks(lines: string[]): RichTextNode[] {
|
|
166
|
+
const out: RichTextNode[] = [];
|
|
167
|
+
let i = 0;
|
|
168
|
+
while (i < lines.length) {
|
|
169
|
+
const line = lines[i];
|
|
170
|
+
if (!line.trim()) { i++; continue; }
|
|
171
|
+
|
|
172
|
+
const fence = FENCE.exec(line);
|
|
173
|
+
if (fence) {
|
|
174
|
+
const body: string[] = [];
|
|
175
|
+
i++;
|
|
176
|
+
while (i < lines.length && !new RegExp(`^ {0,3}${fence[1][0]}{${fence[1].length},}\\s*$`).test(lines[i])) body.push(lines[i++]);
|
|
177
|
+
i++;
|
|
178
|
+
out.push({ type: 'code_block', value: body.join('\n'), ...(fence[2] ? { lang: fence[2] } : {}) });
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
const h = HEADING.exec(line);
|
|
182
|
+
if (h) {
|
|
183
|
+
out.push({ type: `h${h[1].length}`, children: parseInline(h[2] ?? '') });
|
|
184
|
+
i++;
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (HR.test(line)) { out.push({ type: 'hr' }); i++; continue; }
|
|
188
|
+
|
|
189
|
+
if (QUOTE.test(line)) {
|
|
190
|
+
const inner: string[] = [];
|
|
191
|
+
while (i < lines.length && lines[i].trim() && (QUOTE.test(lines[i]) || !startsBlock(lines[i]))) {
|
|
192
|
+
const q = QUOTE.exec(lines[i]);
|
|
193
|
+
inner.push(q ? q[1] : lines[i]);
|
|
194
|
+
i++;
|
|
195
|
+
}
|
|
196
|
+
out.push({ type: 'blockquote', children: parseBlocks(inner) });
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const item = ITEM.exec(line);
|
|
201
|
+
if (item && (item[3] || lines[i + 1]?.startsWith(' '))) {
|
|
202
|
+
const indent = item[1].length;
|
|
203
|
+
const ordered = /\d/.test(item[2]);
|
|
204
|
+
const items: RichTextNode[] = [];
|
|
205
|
+
while (i < lines.length) {
|
|
206
|
+
const m = ITEM.exec(lines[i]);
|
|
207
|
+
if (!m || m[1].length !== indent || /\d/.test(m[2]) !== ordered) break;
|
|
208
|
+
const contentIndent = indent + m[2].length + (m[3] ? m[3].length - (m[4] ?? '').length : 1);
|
|
209
|
+
const body = [m[4] ?? ''];
|
|
210
|
+
i++;
|
|
211
|
+
while (i < lines.length) {
|
|
212
|
+
const next = lines[i];
|
|
213
|
+
if (!next.trim()) {
|
|
214
|
+
// A blank line continues the item only if indented content follows.
|
|
215
|
+
const after = lines.slice(i + 1).find((l) => l.trim());
|
|
216
|
+
if (after !== undefined && after.length - after.trimStart().length >= contentIndent) { body.push(''); i++; continue; }
|
|
217
|
+
break;
|
|
218
|
+
}
|
|
219
|
+
const lead = next.length - next.trimStart().length;
|
|
220
|
+
if (lead >= contentIndent) { body.push(next.slice(contentIndent)); i++; continue; }
|
|
221
|
+
if (ITEM.test(next) && lead <= indent) break;
|
|
222
|
+
if (lead > indent && ITEM.test(next)) { body.push(next.slice(Math.min(lead, contentIndent))); i++; continue; }
|
|
223
|
+
if (startsBlock(next)) break;
|
|
224
|
+
body.push(next.trim()); // lazy continuation of the item's paragraph
|
|
225
|
+
i++;
|
|
226
|
+
}
|
|
227
|
+
const kids = parseBlocks(body).map((k, n) => (n === 0 && k.type === 'p' ? { ...k, type: 'lic' } : k));
|
|
228
|
+
items.push({ type: 'li', children: kids.length ? kids : [{ type: 'lic', children: [] }] });
|
|
229
|
+
while (i < lines.length && !lines[i].trim()) {
|
|
230
|
+
const after = lines.slice(i).find((l) => l.trim());
|
|
231
|
+
const m2 = after ? ITEM.exec(after) : null;
|
|
232
|
+
if (m2 && m2[1].length === indent && /\d/.test(m2[2]) === ordered) i++;
|
|
233
|
+
else break;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
out.push({ type: ordered ? 'ol' : 'ul', children: items });
|
|
237
|
+
continue;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (line.includes('|') && TABLE_RULE.test(lines[i + 1] ?? '') && (lines[i + 1] ?? '').includes('-')) {
|
|
241
|
+
const head = cells(line);
|
|
242
|
+
const rows: RichTextNode[] = [{ type: 'tr', children: head.map((c) => ({ type: 'th', children: parseInline(c) })) }];
|
|
243
|
+
i += 2;
|
|
244
|
+
while (i < lines.length && lines[i].trim() && lines[i].includes('|')) {
|
|
245
|
+
const row = cells(lines[i++]);
|
|
246
|
+
rows.push({ type: 'tr', children: head.map((_, n) => ({ type: 'td', children: parseInline(row[n] ?? '') })) });
|
|
247
|
+
}
|
|
248
|
+
out.push({ type: 'table', children: rows });
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const para: string[] = [line.trim()];
|
|
253
|
+
i++;
|
|
254
|
+
while (i < lines.length && lines[i].trim() && !startsBlock(lines[i]) && !(lines[i].includes('|') && TABLE_RULE.test(lines[i + 1] ?? ''))) para.push(lines[i++].trim());
|
|
255
|
+
const img = para.length === 1 ? IMAGE.exec(para[0]) : null;
|
|
256
|
+
if (img) {
|
|
257
|
+
const url = img[2].startsWith('<') ? img[2].slice(1, -1) : img[2];
|
|
258
|
+
out.push({ type: 'img', url, alt: img[1].replace(/\\(.)/g, '$1'), ...(img[3] ? { caption: img[3].replace(/\\(.)/g, '$1') } : {}) });
|
|
259
|
+
} else out.push({ type: 'p', children: parseInline(para.join('\n')) });
|
|
260
|
+
}
|
|
261
|
+
return out;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/** Parse Markdown into the rich-text tree RichText renders. */
|
|
265
|
+
export function markdownToRichText(markdown: string): RichTextRoot {
|
|
266
|
+
const lines = markdown.replace(/\r\n?/g, '\n').replace(/\t/g, ' ').split('\n');
|
|
267
|
+
return { type: 'root', children: parseBlocks(lines) };
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// --- Tree → Markdown -----------------------------------------------------------------------------
|
|
271
|
+
|
|
272
|
+
const escapeText = (t: string) => t.replace(/([\\`*_[\]~<])/g, '\\$1');
|
|
273
|
+
const escapeLineStart = (line: string) => line.replace(/^(\s*)(#{1,6}\s|>|[-+*]\s|\d+[.)]\s)/, (_, sp: string, m: string) => `${sp}\\${m}`);
|
|
274
|
+
|
|
275
|
+
function inlineMd(nodes: RichTextNode[] = []): string {
|
|
276
|
+
return nodes
|
|
277
|
+
.map((n) => {
|
|
278
|
+
if (!n || typeof n !== 'object') return '';
|
|
279
|
+
if (n.type === 'break') return '\n';
|
|
280
|
+
if (n.type === 'a') {
|
|
281
|
+
const title = n.title ? ` "${String(n.title).replace(/"/g, '\\"')}"` : '';
|
|
282
|
+
return `[${inlineMd(n.children)}](${String(n.url ?? '').replace(/[()\s]/g, encodeURIComponent)}${title})`;
|
|
283
|
+
}
|
|
284
|
+
if (n.type === 'text') {
|
|
285
|
+
const t = n.text ?? '';
|
|
286
|
+
if (!t) return '';
|
|
287
|
+
if (n.code) return t.includes('`') ? `\`\` ${t} \`\`` : `\`${t}\``;
|
|
288
|
+
// Marks wrap the trimmed text so `** bold**` never happens.
|
|
289
|
+
const [, lead, core, trail] = /^(\s*)([\s\S]*?)(\s*)$/.exec(t)!;
|
|
290
|
+
if (!core) return t;
|
|
291
|
+
let s = escapeText(core);
|
|
292
|
+
if (n.strikethrough) s = `~~${s}~~`;
|
|
293
|
+
if (n.italic) s = `*${s}*`;
|
|
294
|
+
if (n.bold) s = `**${s}**`;
|
|
295
|
+
return lead + s + trail;
|
|
296
|
+
}
|
|
297
|
+
return inlineMd(n.children); // unknown inline wrappers: keep their text
|
|
298
|
+
})
|
|
299
|
+
.join('');
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function blockMd(n: RichTextNode, depth = 0): string {
|
|
303
|
+
const kids = n.children ?? [];
|
|
304
|
+
const h = /^h([1-6])$/.exec(n.type);
|
|
305
|
+
if (h) return `${'#'.repeat(Number(h[1]))} ${inlineMd(kids).replace(/\n/g, ' ')}`;
|
|
306
|
+
switch (n.type) {
|
|
307
|
+
case 'p':
|
|
308
|
+
case 'lic':
|
|
309
|
+
return inlineMd(kids).split('\n').map(escapeLineStart).join('\n');
|
|
310
|
+
case 'ul':
|
|
311
|
+
case 'ol':
|
|
312
|
+
return kids
|
|
313
|
+
.map((li, i) => {
|
|
314
|
+
const marker = n.type === 'ol' ? `${i + 1}. ` : '- ';
|
|
315
|
+
const pad = ' '.repeat(marker.length);
|
|
316
|
+
const parts = (li.type === 'li' ? li.children ?? [] : [li]).map((c) => blockMd(c, depth + 1)).filter((s) => s.trim());
|
|
317
|
+
const body = parts.join('\n').split('\n').map((l, j) => (j === 0 ? l : l ? pad + l : l)).join('\n');
|
|
318
|
+
return marker + body;
|
|
319
|
+
})
|
|
320
|
+
.join('\n');
|
|
321
|
+
case 'blockquote':
|
|
322
|
+
return kids.map((k) => blockMd(k)).join('\n\n').split('\n').map((l) => (l ? `> ${l}` : '>')).join('\n');
|
|
323
|
+
case 'code_block': {
|
|
324
|
+
const value = String(n.value ?? '');
|
|
325
|
+
const fence = value.includes('```') ? '~~~' : '```';
|
|
326
|
+
return `${fence}${n.lang ?? ''}\n${value}\n${fence}`;
|
|
327
|
+
}
|
|
328
|
+
case 'hr':
|
|
329
|
+
return '---';
|
|
330
|
+
case 'img':
|
|
331
|
+
return `.replace(/[()\s]/g, encodeURIComponent)}${n.caption ? ` "${String(n.caption).replace(/"/g, '\\"')}"` : ''})`;
|
|
332
|
+
case 'table': {
|
|
333
|
+
const rows = kids.flatMap((k) => (k.type === 'thead' || k.type === 'tbody' ? k.children ?? [] : [k]));
|
|
334
|
+
if (!rows.length) return '';
|
|
335
|
+
const line = (r: RichTextNode) => `| ${(r.children ?? []).map((c) => inlineMd(c.children).replace(/\n/g, ' ').replace(/\|/g, '\\|')).join(' | ')} |`;
|
|
336
|
+
const width = (rows[0].children ?? []).length;
|
|
337
|
+
return [line(rows[0]), `|${' --- |'.repeat(width)}`, ...rows.slice(1).map(line)].join('\n');
|
|
338
|
+
}
|
|
339
|
+
default:
|
|
340
|
+
return kids.length ? inlineMd(kids) : '';
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/** A rich-text tree (or plain text) as Markdown. Unknown node types keep their text. */
|
|
345
|
+
export function richTextToMarkdown(value: RichTextRoot | RichTextNode[] | string | null | undefined): string {
|
|
346
|
+
if (value == null) return '';
|
|
347
|
+
if (typeof value === 'string') return value;
|
|
348
|
+
const nodes = Array.isArray(value) ? value : value.type === 'root' ? value.children ?? [] : [value];
|
|
349
|
+
return nodes.map((n) => blockMd(n)).filter((s) => s.trim()).join('\n\n');
|
|
350
|
+
}
|