@btst/stack 1.0.1 → 1.1.1
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/README.md +156 -709
- package/dist/api/index.cjs +2 -1
- package/dist/api/index.d.cts +4 -3
- package/dist/api/index.d.mts +4 -3
- package/dist/api/index.d.ts +4 -3
- package/dist/api/index.mjs +1 -1
- package/dist/client/components/compose.cjs +68 -0
- package/dist/client/components/compose.mjs +65 -0
- package/dist/client/components/error-boundary.cjs +24 -0
- package/dist/client/components/error-boundary.mjs +22 -0
- package/dist/client/components/index.cjs +10 -0
- package/dist/client/components/index.d.cts +52 -0
- package/dist/client/components/index.d.mts +52 -0
- package/dist/client/components/index.d.ts +52 -0
- package/dist/client/components/index.mjs +2 -0
- package/dist/client/index.cjs +24 -5
- package/dist/client/index.d.cts +125 -8
- package/dist/client/index.d.mts +125 -8
- package/dist/client/index.d.ts +125 -8
- package/dist/client/index.mjs +21 -4
- package/dist/client/meta-utils.cjs +162 -0
- package/dist/client/meta-utils.mjs +160 -0
- package/dist/client/path-utils.cjs +15 -0
- package/dist/client/path-utils.mjs +13 -0
- package/dist/client/sitemap-utils.cjs +14 -0
- package/dist/client/sitemap-utils.mjs +12 -0
- package/dist/context/index.cjs +6 -63
- package/dist/context/index.d.cts +21 -24
- package/dist/context/index.d.mts +21 -24
- package/dist/context/index.d.ts +21 -24
- package/dist/context/index.mjs +1 -61
- package/dist/context/provider.cjs +51 -0
- package/dist/context/provider.mjs +46 -0
- package/dist/index.cjs +2 -3
- package/dist/index.d.cts +3 -2
- package/dist/index.d.mts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.mjs +1 -2
- package/dist/plugins/api/index.cjs +13 -0
- package/dist/plugins/api/index.d.cts +40 -0
- package/dist/plugins/api/index.d.mts +40 -0
- package/dist/plugins/api/index.d.ts +40 -0
- package/dist/plugins/api/index.mjs +8 -0
- package/dist/plugins/blog/api/index.cjs +11 -0
- package/dist/plugins/blog/api/index.d.cts +7 -0
- package/dist/plugins/blog/api/index.d.mts +7 -0
- package/dist/plugins/blog/api/index.d.ts +7 -0
- package/dist/plugins/blog/api/index.mjs +2 -0
- package/dist/plugins/blog/api/plugin.cjs +569 -0
- package/dist/plugins/blog/api/plugin.mjs +565 -0
- package/dist/plugins/blog/client/components/forms/image-field.cjs +133 -0
- package/dist/plugins/blog/client/components/forms/image-field.mjs +131 -0
- package/dist/plugins/blog/client/components/forms/markdown-editor-styles.css +30 -0
- package/dist/plugins/blog/client/components/forms/markdown-editor.cjs +106 -0
- package/dist/plugins/blog/client/components/forms/markdown-editor.mjs +104 -0
- package/dist/plugins/blog/client/components/forms/post-forms.cjs +401 -0
- package/dist/plugins/blog/client/components/forms/post-forms.mjs +398 -0
- package/dist/plugins/blog/client/components/forms/tags-multiselect.cjs +71 -0
- package/dist/plugins/blog/client/components/forms/tags-multiselect.mjs +65 -0
- package/dist/plugins/blog/client/components/index.cjs +17 -0
- package/dist/plugins/blog/client/components/index.d.cts +22 -0
- package/dist/plugins/blog/client/components/index.d.mts +22 -0
- package/dist/plugins/blog/client/components/index.d.ts +22 -0
- package/dist/plugins/blog/client/components/index.mjs +12 -0
- package/dist/plugins/blog/client/components/loading/form-page-skeleton.cjs +62 -0
- package/dist/plugins/blog/client/components/loading/form-page-skeleton.mjs +60 -0
- package/dist/plugins/blog/client/components/loading/index.cjs +20 -0
- package/dist/plugins/blog/client/components/loading/index.mjs +16 -0
- package/dist/plugins/blog/client/components/loading/list-page-skeleton.cjs +26 -0
- package/dist/plugins/blog/client/components/loading/list-page-skeleton.mjs +24 -0
- package/dist/plugins/blog/client/components/loading/page-header-skeleton.cjs +13 -0
- package/dist/plugins/blog/client/components/loading/page-header-skeleton.mjs +11 -0
- package/dist/plugins/blog/client/components/loading/post-card-skeleton.cjs +22 -0
- package/dist/plugins/blog/client/components/loading/post-card-skeleton.mjs +20 -0
- package/dist/plugins/blog/client/components/loading/post-page-skeleton.cjs +56 -0
- package/dist/plugins/blog/client/components/loading/post-page-skeleton.mjs +54 -0
- package/dist/plugins/blog/client/components/pages/404-page.cjs +19 -0
- package/dist/plugins/blog/client/components/pages/404-page.mjs +17 -0
- package/dist/plugins/blog/client/components/pages/edit-post-page.cjs +41 -0
- package/dist/plugins/blog/client/components/pages/edit-post-page.internal.cjs +57 -0
- package/dist/plugins/blog/client/components/pages/edit-post-page.internal.mjs +55 -0
- package/dist/plugins/blog/client/components/pages/edit-post-page.mjs +39 -0
- package/dist/plugins/blog/client/components/pages/home-page.cjs +41 -0
- package/dist/plugins/blog/client/components/pages/home-page.internal.cjs +61 -0
- package/dist/plugins/blog/client/components/pages/home-page.internal.mjs +59 -0
- package/dist/plugins/blog/client/components/pages/home-page.mjs +39 -0
- package/dist/plugins/blog/client/components/pages/new-post-page.cjs +37 -0
- package/dist/plugins/blog/client/components/pages/new-post-page.internal.cjs +53 -0
- package/dist/plugins/blog/client/components/pages/new-post-page.internal.mjs +51 -0
- package/dist/plugins/blog/client/components/pages/new-post-page.mjs +35 -0
- package/dist/plugins/blog/client/components/pages/post-page.cjs +39 -0
- package/dist/plugins/blog/client/components/pages/post-page.internal.cjs +101 -0
- package/dist/plugins/blog/client/components/pages/post-page.internal.mjs +99 -0
- package/dist/plugins/blog/client/components/pages/post-page.mjs +37 -0
- package/dist/plugins/blog/client/components/pages/tag-page.cjs +39 -0
- package/dist/plugins/blog/client/components/pages/tag-page.internal.cjs +61 -0
- package/dist/plugins/blog/client/components/pages/tag-page.internal.mjs +59 -0
- package/dist/plugins/blog/client/components/pages/tag-page.mjs +37 -0
- package/dist/plugins/blog/client/components/shared/better-blog-attribution.cjs +24 -0
- package/dist/plugins/blog/client/components/shared/better-blog-attribution.mjs +22 -0
- package/dist/plugins/blog/client/components/shared/default-error.cjs +18 -0
- package/dist/plugins/blog/client/components/shared/default-error.mjs +16 -0
- package/dist/plugins/blog/client/components/shared/defaults.cjs +13 -0
- package/dist/plugins/blog/client/components/shared/defaults.mjs +10 -0
- package/dist/plugins/blog/client/components/shared/empty-list.cjs +21 -0
- package/dist/plugins/blog/client/components/shared/empty-list.mjs +19 -0
- package/dist/plugins/blog/client/components/shared/error-placeholder.cjs +24 -0
- package/dist/plugins/blog/client/components/shared/error-placeholder.mjs +22 -0
- package/dist/plugins/blog/client/components/shared/highlight-text.cjs +53 -0
- package/dist/plugins/blog/client/components/shared/highlight-text.mjs +51 -0
- package/dist/plugins/blog/client/components/shared/markdown-content-styles.css +328 -0
- package/dist/plugins/blog/client/components/shared/markdown-content.cjs +324 -0
- package/dist/plugins/blog/client/components/shared/markdown-content.mjs +315 -0
- package/dist/plugins/blog/client/components/shared/on-this-page.cjs +161 -0
- package/dist/plugins/blog/client/components/shared/on-this-page.mjs +158 -0
- package/dist/plugins/blog/client/components/shared/page-header.cjs +40 -0
- package/dist/plugins/blog/client/components/shared/page-header.mjs +38 -0
- package/dist/plugins/blog/client/components/shared/page-layout.cjs +24 -0
- package/dist/plugins/blog/client/components/shared/page-layout.mjs +22 -0
- package/dist/plugins/blog/client/components/shared/page-wrapper.cjs +23 -0
- package/dist/plugins/blog/client/components/shared/page-wrapper.mjs +21 -0
- package/dist/plugins/blog/client/components/shared/post-card.cjs +279 -0
- package/dist/plugins/blog/client/components/shared/post-card.mjs +277 -0
- package/dist/plugins/blog/client/components/shared/post-navigation.cjs +74 -0
- package/dist/plugins/blog/client/components/shared/post-navigation.mjs +72 -0
- package/dist/plugins/blog/client/components/shared/posts-list.cjs +48 -0
- package/dist/plugins/blog/client/components/shared/posts-list.mjs +46 -0
- package/dist/plugins/blog/client/components/shared/recent-posts-carousel.cjs +59 -0
- package/dist/plugins/blog/client/components/shared/recent-posts-carousel.mjs +57 -0
- package/dist/plugins/blog/client/components/shared/search-input.cjs +136 -0
- package/dist/plugins/blog/client/components/shared/search-input.mjs +117 -0
- package/dist/plugins/blog/client/components/shared/search-modal.cjs +135 -0
- package/dist/plugins/blog/client/components/shared/search-modal.mjs +116 -0
- package/dist/plugins/blog/client/components/shared/tags-list.cjs +22 -0
- package/dist/plugins/blog/client/components/shared/tags-list.mjs +20 -0
- package/dist/plugins/blog/client/components/shared/use-route-lifecycle.cjs +50 -0
- package/dist/plugins/blog/client/components/shared/use-route-lifecycle.mjs +48 -0
- package/dist/plugins/blog/client/hooks/blog-hooks.cjs +380 -0
- package/dist/plugins/blog/client/hooks/blog-hooks.mjs +368 -0
- package/dist/plugins/blog/client/hooks/index.cjs +17 -0
- package/dist/plugins/blog/client/hooks/index.d.cts +150 -0
- package/dist/plugins/blog/client/hooks/index.d.mts +150 -0
- package/dist/plugins/blog/client/hooks/index.d.ts +150 -0
- package/dist/plugins/blog/client/hooks/index.mjs +1 -0
- package/dist/plugins/blog/client/hooks/use-debounce.cjs +16 -0
- package/dist/plugins/blog/client/hooks/use-debounce.mjs +14 -0
- package/dist/plugins/blog/client/index.cjs +7 -0
- package/dist/plugins/blog/client/index.d.cts +414 -0
- package/dist/plugins/blog/client/index.d.mts +414 -0
- package/dist/plugins/blog/client/index.d.ts +414 -0
- package/dist/plugins/blog/client/index.mjs +1 -0
- package/dist/plugins/blog/client/localization/blog-card.cjs +7 -0
- package/dist/plugins/blog/client/localization/blog-card.mjs +5 -0
- package/dist/plugins/blog/client/localization/blog-common.cjs +10 -0
- package/dist/plugins/blog/client/localization/blog-common.mjs +8 -0
- package/dist/plugins/blog/client/localization/blog-forms.cjs +40 -0
- package/dist/plugins/blog/client/localization/blog-forms.mjs +38 -0
- package/dist/plugins/blog/client/localization/blog-list.cjs +18 -0
- package/dist/plugins/blog/client/localization/blog-list.mjs +16 -0
- package/dist/plugins/blog/client/localization/blog-post.cjs +13 -0
- package/dist/plugins/blog/client/localization/blog-post.mjs +11 -0
- package/dist/plugins/blog/client/localization/index.cjs +17 -0
- package/dist/plugins/blog/client/localization/index.mjs +15 -0
- package/dist/plugins/blog/client/plugin.cjs +462 -0
- package/dist/plugins/blog/client/plugin.mjs +460 -0
- package/dist/plugins/blog/client.css +3 -0
- package/dist/plugins/blog/db.cjs +90 -0
- package/dist/plugins/blog/db.mjs +88 -0
- package/dist/plugins/blog/query-keys.cjs +181 -0
- package/dist/plugins/blog/query-keys.d.cts +530 -0
- package/dist/plugins/blog/query-keys.d.mts +530 -0
- package/dist/plugins/blog/query-keys.d.ts +530 -0
- package/dist/plugins/blog/query-keys.mjs +179 -0
- package/dist/plugins/blog/schemas.cjs +39 -0
- package/dist/plugins/blog/schemas.mjs +35 -0
- package/dist/plugins/blog/style.css +22 -0
- package/dist/plugins/blog/utils.cjs +97 -0
- package/dist/plugins/blog/utils.mjs +87 -0
- package/dist/plugins/client/index.cjs +15 -0
- package/dist/plugins/client/index.d.cts +57 -0
- package/dist/plugins/client/index.d.mts +57 -0
- package/dist/plugins/client/index.d.ts +57 -0
- package/dist/plugins/client/index.mjs +9 -0
- package/dist/{shared/stack.3OUyGp_E.mjs → plugins/utils.mjs} +1 -1
- package/dist/shared/{stack.DORw_1ps.d.cts → stack.ByOugz9d.d.cts} +17 -1
- package/dist/shared/{stack.DORw_1ps.d.mts → stack.ByOugz9d.d.mts} +17 -1
- package/dist/shared/{stack.DORw_1ps.d.ts → stack.ByOugz9d.d.ts} +17 -1
- package/dist/shared/stack.CoPoHVfV.d.cts +76 -0
- package/dist/shared/stack.CoPoHVfV.d.mts +76 -0
- package/dist/shared/stack.CoPoHVfV.d.ts +76 -0
- package/package.json +102 -14
- package/src/__tests__/plugins.test.tsx +539 -0
- package/src/__tests__/sitemap.test.ts +60 -0
- package/src/api/index.ts +75 -0
- package/src/client/components/compose.tsx +116 -0
- package/src/client/components/error-boundary.tsx +30 -0
- package/src/client/components/index.tsx +2 -0
- package/src/client/index.ts +109 -0
- package/src/client/meta-utils.ts +228 -0
- package/src/client/path-utils.ts +38 -0
- package/src/client/sitemap-utils.ts +46 -0
- package/src/context/index.ts +1 -0
- package/src/context/provider.tsx +157 -0
- package/src/index.ts +1 -0
- package/src/plugins/api/index.ts +50 -0
- package/src/plugins/blog/api/index.ts +2 -0
- package/src/plugins/blog/api/plugin.ts +759 -0
- package/src/plugins/blog/client/components/forms/image-field.tsx +165 -0
- package/src/plugins/blog/client/components/forms/markdown-editor-styles.css +30 -0
- package/src/plugins/blog/client/components/forms/markdown-editor.tsx +136 -0
- package/src/plugins/blog/client/components/forms/post-forms.tsx +531 -0
- package/src/plugins/blog/client/components/forms/tags-multiselect.tsx +79 -0
- package/src/plugins/blog/client/components/index.tsx +11 -0
- package/src/plugins/blog/client/components/loading/form-page-skeleton.tsx +75 -0
- package/src/plugins/blog/client/components/loading/index.tsx +27 -0
- package/src/plugins/blog/client/components/loading/list-page-skeleton.tsx +38 -0
- package/src/plugins/blog/client/components/loading/page-header-skeleton.tsx +10 -0
- package/src/plugins/blog/client/components/loading/post-card-skeleton.tsx +30 -0
- package/src/plugins/blog/client/components/loading/post-page-skeleton.tsx +75 -0
- package/src/plugins/blog/client/components/pages/404-page.tsx +23 -0
- package/src/plugins/blog/client/components/pages/edit-post-page.internal.tsx +60 -0
- package/src/plugins/blog/client/components/pages/edit-post-page.tsx +40 -0
- package/src/plugins/blog/client/components/pages/home-page.internal.tsx +71 -0
- package/src/plugins/blog/client/components/pages/home-page.tsx +42 -0
- package/src/plugins/blog/client/components/pages/new-post-page.internal.tsx +59 -0
- package/src/plugins/blog/client/components/pages/new-post-page.tsx +36 -0
- package/src/plugins/blog/client/components/pages/post-page.internal.tsx +142 -0
- package/src/plugins/blog/client/components/pages/post-page.tsx +38 -0
- package/src/plugins/blog/client/components/pages/tag-page.internal.tsx +74 -0
- package/src/plugins/blog/client/components/pages/tag-page.tsx +38 -0
- package/src/plugins/blog/client/components/shared/better-blog-attribution.tsx +19 -0
- package/src/plugins/blog/client/components/shared/default-error.tsx +20 -0
- package/src/plugins/blog/client/components/shared/defaults.tsx +9 -0
- package/src/plugins/blog/client/components/shared/empty-list.tsx +25 -0
- package/src/plugins/blog/client/components/shared/error-placeholder.tsx +20 -0
- package/src/plugins/blog/client/components/shared/highlight-text.tsx +80 -0
- package/src/plugins/blog/client/components/shared/markdown-content-styles.css +328 -0
- package/src/plugins/blog/client/components/shared/markdown-content.tsx +448 -0
- package/src/plugins/blog/client/components/shared/on-this-page.tsx +234 -0
- package/src/plugins/blog/client/components/shared/page-header.tsx +35 -0
- package/src/plugins/blog/client/components/shared/page-layout.tsx +23 -0
- package/src/plugins/blog/client/components/shared/page-wrapper.tsx +32 -0
- package/src/plugins/blog/client/components/shared/post-card.tsx +308 -0
- package/src/plugins/blog/client/components/shared/post-navigation.tsx +98 -0
- package/src/plugins/blog/client/components/shared/posts-list.tsx +67 -0
- package/src/plugins/blog/client/components/shared/recent-posts-carousel.tsx +79 -0
- package/src/plugins/blog/client/components/shared/search-input.tsx +146 -0
- package/src/plugins/blog/client/components/shared/search-modal.tsx +162 -0
- package/src/plugins/blog/client/components/shared/tags-list.tsx +34 -0
- package/src/plugins/blog/client/components/shared/use-route-lifecycle.tsx +68 -0
- package/src/plugins/blog/client/hooks/blog-hooks.tsx +623 -0
- package/src/plugins/blog/client/hooks/index.tsx +1 -0
- package/src/plugins/blog/client/hooks/use-debounce.ts +43 -0
- package/src/plugins/blog/client/index.ts +9 -0
- package/src/plugins/blog/client/localization/blog-card.ts +3 -0
- package/src/plugins/blog/client/localization/blog-common.ts +7 -0
- package/src/plugins/blog/client/localization/blog-forms.ts +45 -0
- package/src/plugins/blog/client/localization/blog-list.ts +14 -0
- package/src/plugins/blog/client/localization/blog-post.ts +9 -0
- package/src/plugins/blog/client/localization/index.ts +15 -0
- package/src/plugins/blog/client/overrides.ts +123 -0
- package/src/plugins/blog/client/plugin.tsx +672 -0
- package/src/plugins/blog/client.css +3 -0
- package/src/plugins/blog/db.ts +90 -0
- package/src/plugins/blog/query-keys.ts +267 -0
- package/src/plugins/blog/schemas.ts +39 -0
- package/src/plugins/blog/style.css +22 -0
- package/src/plugins/blog/types.ts +37 -0
- package/src/plugins/blog/utils.ts +144 -0
- package/src/plugins/client/index.ts +53 -0
- package/src/plugins/index.ts +0 -0
- package/src/plugins/utils.ts +35 -0
- package/src/types.ts +209 -0
- package/dist/plugins/index.cjs +0 -15
- package/dist/plugins/index.d.cts +0 -64
- package/dist/plugins/index.d.mts +0 -64
- package/dist/plugins/index.d.ts +0 -64
- package/dist/plugins/index.mjs +0 -11
- package/dist/shared/stack.DrUAVfIH.d.cts +0 -17
- package/dist/shared/stack.DrUAVfIH.d.mts +0 -17
- package/dist/shared/stack.DrUAVfIH.d.ts +0 -17
- /package/dist/{shared/stack.CktCg4PJ.cjs → plugins/utils.cjs} +0 -0
|
@@ -0,0 +1,315 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
3
|
+
import { cn, slugify } from '../../../utils.mjs';
|
|
4
|
+
import { useState, useMemo, useEffect, createElement, isValidElement, useRef } from 'react';
|
|
5
|
+
import ReactMarkdown from 'react-markdown';
|
|
6
|
+
import rehypeHighlight from 'rehype-highlight';
|
|
7
|
+
import rehypeRaw from 'rehype-raw';
|
|
8
|
+
import remarkGfm from 'remark-gfm';
|
|
9
|
+
import 'highlight.js/styles/panda-syntax-light.css';
|
|
10
|
+
import { CheckIcon, CopyIcon } from 'lucide-react';
|
|
11
|
+
import { usePluginOverrides } from '@btst/stack/context';
|
|
12
|
+
import { DefaultImage, DefaultLink } from './defaults.mjs';
|
|
13
|
+
|
|
14
|
+
function containsMath(markdown) {
|
|
15
|
+
return /\$\$[\s\S]+?\$\$/.test(markdown) || // Display math
|
|
16
|
+
/(?<!\$)\$(?!\$)[^\$\n]+?\$(?!\$)/.test(markdown) || // Inline math (not $$)
|
|
17
|
+
/\\begin\{(equation|align|gather|math)\}/.test(markdown);
|
|
18
|
+
}
|
|
19
|
+
function getNodeText(node) {
|
|
20
|
+
if (node == null) return "";
|
|
21
|
+
if (typeof node === "string" || typeof node === "number") return String(node);
|
|
22
|
+
if (Array.isArray(node)) return node.map(getNodeText).join("");
|
|
23
|
+
if (isValidElement(node)) {
|
|
24
|
+
const props = node.props;
|
|
25
|
+
return getNodeText(props.children);
|
|
26
|
+
}
|
|
27
|
+
return "";
|
|
28
|
+
}
|
|
29
|
+
function simpleHash(str) {
|
|
30
|
+
let hash = 0;
|
|
31
|
+
for (let i = 0; i < str.length; i++) {
|
|
32
|
+
const char = str.charCodeAt(i);
|
|
33
|
+
hash = (hash << 5) - hash + char;
|
|
34
|
+
hash = hash & hash;
|
|
35
|
+
}
|
|
36
|
+
return Math.abs(hash).toString(36).slice(0, 6);
|
|
37
|
+
}
|
|
38
|
+
function isCheckboxElement(node) {
|
|
39
|
+
return isValidElement(node) && node.type === "input" && node.props.type === "checkbox";
|
|
40
|
+
}
|
|
41
|
+
function createTaskListItemRenderer() {
|
|
42
|
+
return function LiRenderer(props) {
|
|
43
|
+
const { className, children, ...rest } = props;
|
|
44
|
+
const isTaskItem = (className ?? "").split(" ").includes("task-list-item");
|
|
45
|
+
if (!isTaskItem) {
|
|
46
|
+
return /* @__PURE__ */ jsx("li", { className, ...rest, children });
|
|
47
|
+
}
|
|
48
|
+
const childArray = Array.isArray(children) ? children : [children];
|
|
49
|
+
const checkboxNode = childArray.find(isCheckboxElement);
|
|
50
|
+
const nonCheckboxChildren = childArray.filter((c) => !isCheckboxElement(c));
|
|
51
|
+
const labelText = getNodeText(nonCheckboxChildren);
|
|
52
|
+
const baseId = slugify(labelText || "task-item");
|
|
53
|
+
const hashSuffix = simpleHash(labelText || "task-item");
|
|
54
|
+
const uniqueId = `${baseId}-${hashSuffix}`;
|
|
55
|
+
return /* @__PURE__ */ jsxs("li", { className, ...rest, children: [
|
|
56
|
+
checkboxNode ? createElement(checkboxNode.type, {
|
|
57
|
+
...checkboxNode.props,
|
|
58
|
+
id: uniqueId,
|
|
59
|
+
"aria-label": labelText || "Task item"
|
|
60
|
+
}) : null,
|
|
61
|
+
/* @__PURE__ */ jsx("label", { htmlFor: uniqueId, children: nonCheckboxChildren })
|
|
62
|
+
] });
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
function createHeadingRenderer(tag) {
|
|
66
|
+
return function HeadingRenderer(props) {
|
|
67
|
+
const { children, ...rest } = props;
|
|
68
|
+
const text = getNodeText(children);
|
|
69
|
+
const id = slugify(text);
|
|
70
|
+
return createElement(
|
|
71
|
+
tag,
|
|
72
|
+
{ id, ...rest },
|
|
73
|
+
children,
|
|
74
|
+
text ? /* @__PURE__ */ jsx(
|
|
75
|
+
"a",
|
|
76
|
+
{
|
|
77
|
+
className: "heading-anchor",
|
|
78
|
+
href: `#${id}`,
|
|
79
|
+
"aria-label": "Link to heading",
|
|
80
|
+
children: "#"
|
|
81
|
+
}
|
|
82
|
+
) : null
|
|
83
|
+
);
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function AnchorRenderer(props) {
|
|
87
|
+
const { Link } = usePluginOverrides("blog", {
|
|
88
|
+
Link: DefaultLink
|
|
89
|
+
});
|
|
90
|
+
const { href = "", children, className: anchorClassName, ...rest } = props;
|
|
91
|
+
return /* @__PURE__ */ jsx(Link, { href, className: anchorClassName, ...rest, children });
|
|
92
|
+
}
|
|
93
|
+
function ImgRenderer(props) {
|
|
94
|
+
const { Image } = usePluginOverrides("blog", {
|
|
95
|
+
Image: DefaultImage
|
|
96
|
+
});
|
|
97
|
+
const {
|
|
98
|
+
src = "",
|
|
99
|
+
alt = "",
|
|
100
|
+
className: imgClassName,
|
|
101
|
+
width,
|
|
102
|
+
height,
|
|
103
|
+
style,
|
|
104
|
+
...rest
|
|
105
|
+
} = props;
|
|
106
|
+
const imageProps = {
|
|
107
|
+
src,
|
|
108
|
+
alt,
|
|
109
|
+
className: imgClassName,
|
|
110
|
+
style,
|
|
111
|
+
...rest
|
|
112
|
+
};
|
|
113
|
+
if (width != null && height != null) {
|
|
114
|
+
imageProps.width = width;
|
|
115
|
+
imageProps.height = height;
|
|
116
|
+
} else {
|
|
117
|
+
return /* @__PURE__ */ jsx("span", { className: "markdown-image-wrapper", children: /* @__PURE__ */ jsx(Image, { ...imageProps }) });
|
|
118
|
+
}
|
|
119
|
+
return /* @__PURE__ */ jsx(Image, { ...imageProps });
|
|
120
|
+
}
|
|
121
|
+
function CodeRenderer({ inline, className, children, ...rest }) {
|
|
122
|
+
const hasLanguage = /language-([a-z0-9-]+)/i.test(className ?? "");
|
|
123
|
+
const isInline = inline ?? !hasLanguage;
|
|
124
|
+
if (isInline) {
|
|
125
|
+
return /* @__PURE__ */ jsx("code", { className, ...rest, children });
|
|
126
|
+
}
|
|
127
|
+
return /* @__PURE__ */ jsx("code", { className, ...rest, children });
|
|
128
|
+
}
|
|
129
|
+
function PreRenderer(props) {
|
|
130
|
+
const { children, ...rest } = props;
|
|
131
|
+
const child = Array.isArray(children) ? children[0] : children;
|
|
132
|
+
let language;
|
|
133
|
+
if (isValidElement(child)) {
|
|
134
|
+
const className = child.props.className;
|
|
135
|
+
const match = /language-([a-z0-9-]+)/i.exec(className ?? "");
|
|
136
|
+
language = match?.[1];
|
|
137
|
+
}
|
|
138
|
+
const label = (language ?? "text").toUpperCase();
|
|
139
|
+
const preRef = useRef(null);
|
|
140
|
+
const [copied, setCopied] = useState(false);
|
|
141
|
+
const resetTimerRef = useRef(null);
|
|
142
|
+
let codeText = "";
|
|
143
|
+
if (isValidElement(child)) {
|
|
144
|
+
const childProps = child.props;
|
|
145
|
+
codeText = getNodeText(childProps.children);
|
|
146
|
+
}
|
|
147
|
+
const normalized = codeText.endsWith("\n") ? codeText.slice(0, -1) : codeText;
|
|
148
|
+
const lineCount = Math.max(1, normalized.split("\n").length);
|
|
149
|
+
const digitCount = String(lineCount).length;
|
|
150
|
+
const onCopy = async (e) => {
|
|
151
|
+
e.preventDefault();
|
|
152
|
+
e.stopPropagation();
|
|
153
|
+
try {
|
|
154
|
+
const text = preRef.current?.textContent ?? "";
|
|
155
|
+
if (text && typeof navigator !== "undefined" && navigator.clipboard?.writeText) {
|
|
156
|
+
await navigator.clipboard.writeText(text);
|
|
157
|
+
setCopied(true);
|
|
158
|
+
if (resetTimerRef.current) window.clearTimeout(resetTimerRef.current);
|
|
159
|
+
resetTimerRef.current = window.setTimeout(() => {
|
|
160
|
+
setCopied(false);
|
|
161
|
+
resetTimerRef.current = null;
|
|
162
|
+
}, 2e3);
|
|
163
|
+
}
|
|
164
|
+
} catch {
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
return /* @__PURE__ */ jsxs(
|
|
168
|
+
"div",
|
|
169
|
+
{
|
|
170
|
+
className: "milkdown-code-block",
|
|
171
|
+
style: {
|
|
172
|
+
["--code-gutter-width"]: `${digitCount + 1}ch`
|
|
173
|
+
},
|
|
174
|
+
children: [
|
|
175
|
+
/* @__PURE__ */ jsxs("div", { className: "code-toolbar", children: [
|
|
176
|
+
/* @__PURE__ */ jsx("span", { className: "language-label", children: label }),
|
|
177
|
+
/* @__PURE__ */ jsx(
|
|
178
|
+
"button",
|
|
179
|
+
{
|
|
180
|
+
type: "button",
|
|
181
|
+
className: "copy-button",
|
|
182
|
+
onClick: onCopy,
|
|
183
|
+
"aria-label": copied ? "Copied" : "Copy code",
|
|
184
|
+
children: copied ? /* @__PURE__ */ jsx(CheckIcon, { size: 16 }) : /* @__PURE__ */ jsx(CopyIcon, { size: 16 })
|
|
185
|
+
}
|
|
186
|
+
)
|
|
187
|
+
] }),
|
|
188
|
+
/* @__PURE__ */ jsxs("div", { className: "code-content", children: [
|
|
189
|
+
/* @__PURE__ */ jsx("ol", { className: "line-numbers", "aria-hidden": true, children: Array.from({ length: lineCount }).map((_, idx) => /* @__PURE__ */ jsx("li", { children: idx + 1 }, idx + 1)) }),
|
|
190
|
+
/* @__PURE__ */ jsx("pre", { ref: preRef, ...rest, children })
|
|
191
|
+
] })
|
|
192
|
+
]
|
|
193
|
+
}
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
function MarkdownContent({ markdown, className }) {
|
|
197
|
+
const [mathPlugins, setMathPlugins] = useState(null);
|
|
198
|
+
const [isLoadingMath, setIsLoadingMath] = useState(false);
|
|
199
|
+
const hasMath = useMemo(() => containsMath(markdown), [markdown]);
|
|
200
|
+
useEffect(() => {
|
|
201
|
+
if (!hasMath || mathPlugins || isLoadingMath) return;
|
|
202
|
+
setIsLoadingMath(true);
|
|
203
|
+
const katexCSSId = "katex-css";
|
|
204
|
+
if (!document.getElementById(katexCSSId)) {
|
|
205
|
+
const link = document.createElement("link");
|
|
206
|
+
link.id = katexCSSId;
|
|
207
|
+
link.rel = "stylesheet";
|
|
208
|
+
link.href = "https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css";
|
|
209
|
+
link.integrity = "sha384-nB0miv6/jRmo5UMMR1wu3Gz6NLsoTkbqJghGIsx//Rlm+ZU03BU6SQNC66uf4l5+";
|
|
210
|
+
link.crossOrigin = "anonymous";
|
|
211
|
+
document.head.appendChild(link);
|
|
212
|
+
const fontDisplayStyleId = "katex-font-display-override";
|
|
213
|
+
if (!document.getElementById(fontDisplayStyleId)) {
|
|
214
|
+
const style = document.createElement("style");
|
|
215
|
+
style.id = fontDisplayStyleId;
|
|
216
|
+
style.textContent = `
|
|
217
|
+
/* Override KaTeX font-face declarations to add font-display: swap */
|
|
218
|
+
@font-face {
|
|
219
|
+
font-family: 'KaTeX_Main';
|
|
220
|
+
font-style: normal;
|
|
221
|
+
font-weight: normal;
|
|
222
|
+
font-display: swap;
|
|
223
|
+
src: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Main-Regular.woff2') format('woff2');
|
|
224
|
+
}
|
|
225
|
+
@font-face {
|
|
226
|
+
font-family: 'KaTeX_Math';
|
|
227
|
+
font-style: italic;
|
|
228
|
+
font-weight: normal;
|
|
229
|
+
font-display: swap;
|
|
230
|
+
src: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Math-Italic.woff2') format('woff2');
|
|
231
|
+
}
|
|
232
|
+
@font-face {
|
|
233
|
+
font-family: 'KaTeX_Size1';
|
|
234
|
+
font-style: normal;
|
|
235
|
+
font-weight: normal;
|
|
236
|
+
font-display: swap;
|
|
237
|
+
src: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Size1-Regular.woff2') format('woff2');
|
|
238
|
+
}
|
|
239
|
+
@font-face {
|
|
240
|
+
font-family: 'KaTeX_Size2';
|
|
241
|
+
font-style: normal;
|
|
242
|
+
font-weight: normal;
|
|
243
|
+
font-display: swap;
|
|
244
|
+
src: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Size2-Regular.woff2') format('woff2');
|
|
245
|
+
}
|
|
246
|
+
@font-face {
|
|
247
|
+
font-family: 'KaTeX_Size3';
|
|
248
|
+
font-style: normal;
|
|
249
|
+
font-weight: normal;
|
|
250
|
+
font-display: swap;
|
|
251
|
+
src: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Size3-Regular.woff2') format('woff2');
|
|
252
|
+
}
|
|
253
|
+
@font-face {
|
|
254
|
+
font-family: 'KaTeX_Size4';
|
|
255
|
+
font-style: normal;
|
|
256
|
+
font-weight: normal;
|
|
257
|
+
font-display: swap;
|
|
258
|
+
src: url('https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/fonts/KaTeX_Size4-Regular.woff2') format('woff2');
|
|
259
|
+
}
|
|
260
|
+
`;
|
|
261
|
+
document.head.appendChild(style);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
Promise.all([import('remark-math'), import('rehype-katex')]).then(([remarkMathModule, rehypeKatexModule]) => {
|
|
265
|
+
setMathPlugins({
|
|
266
|
+
remarkMath: remarkMathModule.default,
|
|
267
|
+
rehypeKatex: rehypeKatexModule.default
|
|
268
|
+
});
|
|
269
|
+
}).catch((error) => {
|
|
270
|
+
console.error("Failed to load math plugins:", error);
|
|
271
|
+
}).finally(() => {
|
|
272
|
+
setIsLoadingMath(false);
|
|
273
|
+
});
|
|
274
|
+
}, [hasMath, mathPlugins, isLoadingMath]);
|
|
275
|
+
const components = useMemo(() => {
|
|
276
|
+
return {
|
|
277
|
+
a: AnchorRenderer,
|
|
278
|
+
img: ImgRenderer,
|
|
279
|
+
code: CodeRenderer,
|
|
280
|
+
pre: PreRenderer,
|
|
281
|
+
h1: createHeadingRenderer("h1"),
|
|
282
|
+
h2: createHeadingRenderer("h2"),
|
|
283
|
+
h3: createHeadingRenderer("h3"),
|
|
284
|
+
h4: createHeadingRenderer("h4"),
|
|
285
|
+
h5: createHeadingRenderer("h5"),
|
|
286
|
+
h6: createHeadingRenderer("h6"),
|
|
287
|
+
li: createTaskListItemRenderer()
|
|
288
|
+
};
|
|
289
|
+
}, []);
|
|
290
|
+
const remarkPlugins = useMemo(() => {
|
|
291
|
+
const plugins = [remarkGfm];
|
|
292
|
+
if (hasMath && mathPlugins?.remarkMath) {
|
|
293
|
+
plugins.push(mathPlugins.remarkMath);
|
|
294
|
+
}
|
|
295
|
+
return plugins;
|
|
296
|
+
}, [hasMath, mathPlugins]);
|
|
297
|
+
const rehypePlugins = useMemo(() => {
|
|
298
|
+
const plugins = [rehypeRaw, rehypeHighlight];
|
|
299
|
+
if (hasMath && mathPlugins?.rehypeKatex) {
|
|
300
|
+
plugins.push(mathPlugins.rehypeKatex);
|
|
301
|
+
}
|
|
302
|
+
return plugins;
|
|
303
|
+
}, [hasMath, mathPlugins]);
|
|
304
|
+
return /* @__PURE__ */ jsx("div", { className: cn("milkdown-custom", className), children: /* @__PURE__ */ jsx("div", { className: "milkdown", children: /* @__PURE__ */ jsx("div", { className: "milkdown-content", children: /* @__PURE__ */ jsx(
|
|
305
|
+
ReactMarkdown,
|
|
306
|
+
{
|
|
307
|
+
remarkPlugins,
|
|
308
|
+
rehypePlugins,
|
|
309
|
+
components,
|
|
310
|
+
children: markdown
|
|
311
|
+
}
|
|
312
|
+
) }) }) });
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export { MarkdownContent };
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const jsxRuntime = require('react/jsx-runtime');
|
|
5
|
+
const React = require('react');
|
|
6
|
+
const utils = require('../../../utils.cjs');
|
|
7
|
+
const context = require('@btst/stack/context');
|
|
8
|
+
const index = require('../../localization/index.cjs');
|
|
9
|
+
const select = require('@workspace/ui/components/select');
|
|
10
|
+
|
|
11
|
+
function OnThisPage({ markdown, className }) {
|
|
12
|
+
const { localization } = context.usePluginOverrides("blog", {
|
|
13
|
+
localization: index.BLOG_LOCALIZATION
|
|
14
|
+
});
|
|
15
|
+
const headings = React.useMemo(() => extractHeadings(markdown), [markdown]);
|
|
16
|
+
const activeId = useActiveHeading(headings);
|
|
17
|
+
if (headings.length === 0) {
|
|
18
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
19
|
+
"div",
|
|
20
|
+
{
|
|
21
|
+
className: utils.cn(
|
|
22
|
+
"sticky top-20 z-30 ml-auto hidden xl:flex w-44",
|
|
23
|
+
className
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
const handleClick = (id) => {
|
|
29
|
+
const element = document.getElementById(id);
|
|
30
|
+
if (element) {
|
|
31
|
+
element.scrollIntoView({ behavior: "smooth", block: "start" });
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
35
|
+
"nav",
|
|
36
|
+
{
|
|
37
|
+
className: utils.cn(
|
|
38
|
+
"sticky top-20 z-30 ml-auto hidden xl:flex w-44 flex-col gap-2 overflow-hidden pb-8",
|
|
39
|
+
className
|
|
40
|
+
),
|
|
41
|
+
"aria-label": "Table of contents",
|
|
42
|
+
children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "overflow-y-auto px-2", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-2 p-4 pt-0 text-sm", children: [
|
|
43
|
+
/* @__PURE__ */ jsxRuntime.jsx("p", { className: "font-semibold text-muted-foreground sticky top-0 h-6 text-xs", children: localization.BLOG_POST_ON_THIS_PAGE }),
|
|
44
|
+
headings.map(({ id, text, level }) => {
|
|
45
|
+
const paddingLeft = level === 1 ? "0" : level === 2 ? "0.5rem" : level === 3 ? "1rem" : level === 4 ? "1.5rem" : level === 5 ? "2rem" : level === 6 ? "2.5rem" : "0";
|
|
46
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
47
|
+
"a",
|
|
48
|
+
{
|
|
49
|
+
href: `#${id}`,
|
|
50
|
+
onClick: (e) => {
|
|
51
|
+
e.preventDefault();
|
|
52
|
+
handleClick(id);
|
|
53
|
+
},
|
|
54
|
+
style: { paddingLeft },
|
|
55
|
+
className: utils.cn(
|
|
56
|
+
"text-muted-foreground hover:text-foreground text-[0.8rem] no-underline transition-colors",
|
|
57
|
+
activeId === id && "text-foreground"
|
|
58
|
+
),
|
|
59
|
+
"data-active": activeId === id,
|
|
60
|
+
"data-depth": level,
|
|
61
|
+
children: text
|
|
62
|
+
},
|
|
63
|
+
id
|
|
64
|
+
);
|
|
65
|
+
})
|
|
66
|
+
] }) })
|
|
67
|
+
}
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
function OnThisPageSelect({ markdown, className }) {
|
|
71
|
+
const { localization } = context.usePluginOverrides("blog", {
|
|
72
|
+
localization: index.BLOG_LOCALIZATION
|
|
73
|
+
});
|
|
74
|
+
const headings = React.useMemo(() => extractHeadings(markdown), [markdown]);
|
|
75
|
+
const initialValue = React.useMemo(() => headings[0]?.id ?? "", [headings]);
|
|
76
|
+
const activeId = useActiveHeading(headings);
|
|
77
|
+
const value = activeId || initialValue;
|
|
78
|
+
if (headings.length === 0) {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
const handleValueChange = (id) => {
|
|
82
|
+
const element = document.getElementById(id);
|
|
83
|
+
if (element) {
|
|
84
|
+
element.scrollIntoView({ behavior: "smooth", block: "start" });
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
88
|
+
"div",
|
|
89
|
+
{
|
|
90
|
+
className: utils.cn(
|
|
91
|
+
"sticky z-30 w-full self-stretch xl:hidden bg-background/95 backdrop-blur supports-backdrop-filter:bg-background/60",
|
|
92
|
+
className
|
|
93
|
+
),
|
|
94
|
+
style: {
|
|
95
|
+
top: "var(--navbar-height, 16px)"
|
|
96
|
+
},
|
|
97
|
+
children: /* @__PURE__ */ jsxRuntime.jsxs(select.Select, { value, onValueChange: handleValueChange, children: [
|
|
98
|
+
/* @__PURE__ */ jsxRuntime.jsx(select.SelectTrigger, { className: "w-full", children: /* @__PURE__ */ jsxRuntime.jsx(select.SelectValue, { placeholder: localization.BLOG_POST_ON_THIS_PAGE }) }),
|
|
99
|
+
/* @__PURE__ */ jsxRuntime.jsx(select.SelectContent, { children: headings.map(({ id, text, level }) => {
|
|
100
|
+
const indent = level > 1 ? `${(level - 1) * 0.5}rem` : "0";
|
|
101
|
+
return /* @__PURE__ */ jsxRuntime.jsx(select.SelectItem, { value: id, children: /* @__PURE__ */ jsxRuntime.jsx("span", { style: { paddingLeft: indent }, children: text }) }, id);
|
|
102
|
+
}) })
|
|
103
|
+
] })
|
|
104
|
+
}
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
function extractHeadings(markdown) {
|
|
108
|
+
const headings = [];
|
|
109
|
+
const lines = markdown.split("\n");
|
|
110
|
+
for (const line of lines) {
|
|
111
|
+
const match = line.match(/^(#{1,6})\s+(.+)$/);
|
|
112
|
+
if (match) {
|
|
113
|
+
const level = match[1]?.length ?? 0;
|
|
114
|
+
const text = match[2]?.trim() ?? "";
|
|
115
|
+
const cleanText = text.replace(/\s*#+\s*$/, "").trim();
|
|
116
|
+
const id = utils.slugify(cleanText);
|
|
117
|
+
if (id && cleanText) {
|
|
118
|
+
headings.push({ id, text: cleanText, level });
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return headings;
|
|
123
|
+
}
|
|
124
|
+
function useActiveHeading(headings, onActiveChange) {
|
|
125
|
+
const [activeId, setActiveId] = React.useState("");
|
|
126
|
+
const onActiveChangeRef = React.useRef(onActiveChange);
|
|
127
|
+
React.useEffect(() => {
|
|
128
|
+
onActiveChangeRef.current = onActiveChange;
|
|
129
|
+
}, [onActiveChange]);
|
|
130
|
+
React.useEffect(() => {
|
|
131
|
+
if (headings.length === 0) return;
|
|
132
|
+
const observer = new IntersectionObserver(
|
|
133
|
+
(entries) => {
|
|
134
|
+
for (const entry of entries) {
|
|
135
|
+
if (entry.isIntersecting) {
|
|
136
|
+
setActiveId(entry.target.id);
|
|
137
|
+
onActiveChangeRef.current?.(entry.target.id);
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
{
|
|
143
|
+
rootMargin: "-80px 0px -80% 0px",
|
|
144
|
+
threshold: 0
|
|
145
|
+
}
|
|
146
|
+
);
|
|
147
|
+
headings.forEach(({ id }) => {
|
|
148
|
+
const element = document.getElementById(id);
|
|
149
|
+
if (element) {
|
|
150
|
+
observer.observe(element);
|
|
151
|
+
}
|
|
152
|
+
});
|
|
153
|
+
return () => {
|
|
154
|
+
observer.disconnect();
|
|
155
|
+
};
|
|
156
|
+
}, [headings]);
|
|
157
|
+
return activeId;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
exports.OnThisPage = OnThisPage;
|
|
161
|
+
exports.OnThisPageSelect = OnThisPageSelect;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx, jsxs } from 'react/jsx-runtime';
|
|
3
|
+
import { useMemo, useState, useRef, useEffect } from 'react';
|
|
4
|
+
import { cn, slugify } from '../../../utils.mjs';
|
|
5
|
+
import { usePluginOverrides } from '@btst/stack/context';
|
|
6
|
+
import { BLOG_LOCALIZATION } from '../../localization/index.mjs';
|
|
7
|
+
import { Select, SelectTrigger, SelectValue, SelectContent, SelectItem } from '@workspace/ui/components/select';
|
|
8
|
+
|
|
9
|
+
function OnThisPage({ markdown, className }) {
|
|
10
|
+
const { localization } = usePluginOverrides("blog", {
|
|
11
|
+
localization: BLOG_LOCALIZATION
|
|
12
|
+
});
|
|
13
|
+
const headings = useMemo(() => extractHeadings(markdown), [markdown]);
|
|
14
|
+
const activeId = useActiveHeading(headings);
|
|
15
|
+
if (headings.length === 0) {
|
|
16
|
+
return /* @__PURE__ */ jsx(
|
|
17
|
+
"div",
|
|
18
|
+
{
|
|
19
|
+
className: cn(
|
|
20
|
+
"sticky top-20 z-30 ml-auto hidden xl:flex w-44",
|
|
21
|
+
className
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
const handleClick = (id) => {
|
|
27
|
+
const element = document.getElementById(id);
|
|
28
|
+
if (element) {
|
|
29
|
+
element.scrollIntoView({ behavior: "smooth", block: "start" });
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
return /* @__PURE__ */ jsx(
|
|
33
|
+
"nav",
|
|
34
|
+
{
|
|
35
|
+
className: cn(
|
|
36
|
+
"sticky top-20 z-30 ml-auto hidden xl:flex w-44 flex-col gap-2 overflow-hidden pb-8",
|
|
37
|
+
className
|
|
38
|
+
),
|
|
39
|
+
"aria-label": "Table of contents",
|
|
40
|
+
children: /* @__PURE__ */ jsx("div", { className: "overflow-y-auto px-2", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-2 p-4 pt-0 text-sm", children: [
|
|
41
|
+
/* @__PURE__ */ jsx("p", { className: "font-semibold text-muted-foreground sticky top-0 h-6 text-xs", children: localization.BLOG_POST_ON_THIS_PAGE }),
|
|
42
|
+
headings.map(({ id, text, level }) => {
|
|
43
|
+
const paddingLeft = level === 1 ? "0" : level === 2 ? "0.5rem" : level === 3 ? "1rem" : level === 4 ? "1.5rem" : level === 5 ? "2rem" : level === 6 ? "2.5rem" : "0";
|
|
44
|
+
return /* @__PURE__ */ jsx(
|
|
45
|
+
"a",
|
|
46
|
+
{
|
|
47
|
+
href: `#${id}`,
|
|
48
|
+
onClick: (e) => {
|
|
49
|
+
e.preventDefault();
|
|
50
|
+
handleClick(id);
|
|
51
|
+
},
|
|
52
|
+
style: { paddingLeft },
|
|
53
|
+
className: cn(
|
|
54
|
+
"text-muted-foreground hover:text-foreground text-[0.8rem] no-underline transition-colors",
|
|
55
|
+
activeId === id && "text-foreground"
|
|
56
|
+
),
|
|
57
|
+
"data-active": activeId === id,
|
|
58
|
+
"data-depth": level,
|
|
59
|
+
children: text
|
|
60
|
+
},
|
|
61
|
+
id
|
|
62
|
+
);
|
|
63
|
+
})
|
|
64
|
+
] }) })
|
|
65
|
+
}
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
function OnThisPageSelect({ markdown, className }) {
|
|
69
|
+
const { localization } = usePluginOverrides("blog", {
|
|
70
|
+
localization: BLOG_LOCALIZATION
|
|
71
|
+
});
|
|
72
|
+
const headings = useMemo(() => extractHeadings(markdown), [markdown]);
|
|
73
|
+
const initialValue = useMemo(() => headings[0]?.id ?? "", [headings]);
|
|
74
|
+
const activeId = useActiveHeading(headings);
|
|
75
|
+
const value = activeId || initialValue;
|
|
76
|
+
if (headings.length === 0) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
const handleValueChange = (id) => {
|
|
80
|
+
const element = document.getElementById(id);
|
|
81
|
+
if (element) {
|
|
82
|
+
element.scrollIntoView({ behavior: "smooth", block: "start" });
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
return /* @__PURE__ */ jsx(
|
|
86
|
+
"div",
|
|
87
|
+
{
|
|
88
|
+
className: cn(
|
|
89
|
+
"sticky z-30 w-full self-stretch xl:hidden bg-background/95 backdrop-blur supports-backdrop-filter:bg-background/60",
|
|
90
|
+
className
|
|
91
|
+
),
|
|
92
|
+
style: {
|
|
93
|
+
top: "var(--navbar-height, 16px)"
|
|
94
|
+
},
|
|
95
|
+
children: /* @__PURE__ */ jsxs(Select, { value, onValueChange: handleValueChange, children: [
|
|
96
|
+
/* @__PURE__ */ jsx(SelectTrigger, { className: "w-full", children: /* @__PURE__ */ jsx(SelectValue, { placeholder: localization.BLOG_POST_ON_THIS_PAGE }) }),
|
|
97
|
+
/* @__PURE__ */ jsx(SelectContent, { children: headings.map(({ id, text, level }) => {
|
|
98
|
+
const indent = level > 1 ? `${(level - 1) * 0.5}rem` : "0";
|
|
99
|
+
return /* @__PURE__ */ jsx(SelectItem, { value: id, children: /* @__PURE__ */ jsx("span", { style: { paddingLeft: indent }, children: text }) }, id);
|
|
100
|
+
}) })
|
|
101
|
+
] })
|
|
102
|
+
}
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
function extractHeadings(markdown) {
|
|
106
|
+
const headings = [];
|
|
107
|
+
const lines = markdown.split("\n");
|
|
108
|
+
for (const line of lines) {
|
|
109
|
+
const match = line.match(/^(#{1,6})\s+(.+)$/);
|
|
110
|
+
if (match) {
|
|
111
|
+
const level = match[1]?.length ?? 0;
|
|
112
|
+
const text = match[2]?.trim() ?? "";
|
|
113
|
+
const cleanText = text.replace(/\s*#+\s*$/, "").trim();
|
|
114
|
+
const id = slugify(cleanText);
|
|
115
|
+
if (id && cleanText) {
|
|
116
|
+
headings.push({ id, text: cleanText, level });
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return headings;
|
|
121
|
+
}
|
|
122
|
+
function useActiveHeading(headings, onActiveChange) {
|
|
123
|
+
const [activeId, setActiveId] = useState("");
|
|
124
|
+
const onActiveChangeRef = useRef(onActiveChange);
|
|
125
|
+
useEffect(() => {
|
|
126
|
+
onActiveChangeRef.current = onActiveChange;
|
|
127
|
+
}, [onActiveChange]);
|
|
128
|
+
useEffect(() => {
|
|
129
|
+
if (headings.length === 0) return;
|
|
130
|
+
const observer = new IntersectionObserver(
|
|
131
|
+
(entries) => {
|
|
132
|
+
for (const entry of entries) {
|
|
133
|
+
if (entry.isIntersecting) {
|
|
134
|
+
setActiveId(entry.target.id);
|
|
135
|
+
onActiveChangeRef.current?.(entry.target.id);
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
{
|
|
141
|
+
rootMargin: "-80px 0px -80% 0px",
|
|
142
|
+
threshold: 0
|
|
143
|
+
}
|
|
144
|
+
);
|
|
145
|
+
headings.forEach(({ id }) => {
|
|
146
|
+
const element = document.getElementById(id);
|
|
147
|
+
if (element) {
|
|
148
|
+
observer.observe(element);
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
return () => {
|
|
152
|
+
observer.disconnect();
|
|
153
|
+
};
|
|
154
|
+
}, [headings]);
|
|
155
|
+
return activeId;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export { OnThisPage, OnThisPageSelect };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const jsxRuntime = require('react/jsx-runtime');
|
|
4
|
+
|
|
5
|
+
function PageHeader({
|
|
6
|
+
title,
|
|
7
|
+
description,
|
|
8
|
+
childrenTop,
|
|
9
|
+
childrenBottom
|
|
10
|
+
}) {
|
|
11
|
+
return /* @__PURE__ */ jsxRuntime.jsxs(
|
|
12
|
+
"div",
|
|
13
|
+
{
|
|
14
|
+
className: "flex max-w-2xl flex-col items-center lg:gap-4 gap-2 text-center wrap-anywhere",
|
|
15
|
+
"data-testid": "page-header",
|
|
16
|
+
children: [
|
|
17
|
+
childrenTop,
|
|
18
|
+
/* @__PURE__ */ jsxRuntime.jsx(
|
|
19
|
+
"h1",
|
|
20
|
+
{
|
|
21
|
+
className: "font-medium font-sans lg:text-6xl text-4xl tracking-tight",
|
|
22
|
+
"data-testid": "page-title",
|
|
23
|
+
children: title
|
|
24
|
+
}
|
|
25
|
+
),
|
|
26
|
+
description && /* @__PURE__ */ jsxRuntime.jsx(
|
|
27
|
+
"p",
|
|
28
|
+
{
|
|
29
|
+
className: "text-muted-foreground wrap-anywhere",
|
|
30
|
+
"data-testid": "page-description",
|
|
31
|
+
children: description
|
|
32
|
+
}
|
|
33
|
+
),
|
|
34
|
+
childrenBottom
|
|
35
|
+
]
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
exports.PageHeader = PageHeader;
|