@brillout/docpress 0.15.9 → 0.15.10-commit-ef0b9a0

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.
Files changed (41) hide show
  1. package/NavItemComponent.tsx +12 -0
  2. package/components/CodeSnippets.tsx +68 -0
  3. package/components/index.ts +1 -0
  4. package/detypePlugin.ts +68 -0
  5. package/dist/+config.js +1 -1
  6. package/dist/NavItemComponent.js +38 -46
  7. package/dist/components/CodeBlockTransformer.js +2 -3
  8. package/dist/components/CodeSnippets.d.ts +13 -0
  9. package/dist/components/CodeSnippets.js +39 -0
  10. package/dist/components/Comment.js +1 -2
  11. package/dist/components/FileRemoved.js +4 -6
  12. package/dist/components/HorizontalLine.js +1 -2
  13. package/dist/components/ImportMeta.js +2 -3
  14. package/dist/components/Link.js +34 -50
  15. package/dist/components/Note.js +17 -29
  16. package/dist/components/P.js +1 -12
  17. package/dist/components/RepoLink.js +7 -9
  18. package/dist/components/index.d.ts +1 -0
  19. package/dist/components/index.js +1 -0
  20. package/dist/determineNavItemsColumnLayout.js +48 -62
  21. package/dist/detypePlugin.d.ts +3 -0
  22. package/dist/detypePlugin.js +53 -0
  23. package/dist/parseMarkdownMini.js +5 -17
  24. package/dist/parsePageSections.js +41 -82
  25. package/dist/renderer/usePageContext.js +6 -7
  26. package/dist/resolvePageContext.js +91 -103
  27. package/dist/utils/Emoji/Emoji.js +13 -21
  28. package/dist/utils/assert.js +14 -16
  29. package/dist/utils/cls.js +1 -1
  30. package/dist/utils/determineSectionUrlHash.js +5 -5
  31. package/dist/utils/filter.js +2 -2
  32. package/dist/utils/getGlobalObject.js +3 -3
  33. package/dist/utils/useSelectedLanguage.d.ts +7 -0
  34. package/dist/utils/useSelectedLanguage.js +48 -0
  35. package/dist/vite.config.js +9 -7
  36. package/global.d.ts +2 -0
  37. package/index.ts +3 -0
  38. package/package.json +2 -1
  39. package/tsconfig.json +1 -0
  40. package/utils/useSelectedLanguage.ts +61 -0
  41. package/vite.config.ts +2 -0
@@ -9,6 +9,18 @@ import { assert, assertWarning, jsxToTextContent } from './utils/server'
9
9
  import './NavItemComponent.css'
10
10
  import { parseMarkdownMini } from './parseMarkdownMini'
11
11
 
12
+ /*
13
+ // We cannot do that: we must use `import type` otherwise Vite will transpile global.d.ts and throw:
14
+ // ```console
15
+ // [11:55:47.528][/docs/.test-dev.test.ts][pnpm run dev][stderr] 11:55:47 AM [vite] Failed to transpile /home/runner/work/telefunc/telefunc/node_modules/.pnpm/@brillout+docpress@0.15.7_@algolia+client-search@5.31.0_@types+react@19.1.8_@vitejs+plugin-re_lcm3fspejcg3ebrmr3gvb5i3se/node_modules/@brillout/docpress/global.d.ts because:
16
+ // x `declare` modifier not allowed for code already in an ambient context
17
+ // ```
18
+ import './global.d.ts'
19
+ /*/
20
+ // The only purpose of `FakeExport` is to be able to use `import type`
21
+ import type { FakeExport } from './global.d.ts'
22
+ //*/
23
+
12
24
  type NavItemComputed = ReturnType<typeof getNavItemsWithComputed>[number]
13
25
  type NavItem = {
14
26
  level: number
@@ -0,0 +1,68 @@
1
+ export { CodeSnippets, CodeSnippet, TypescriptOnly }
2
+
3
+ import React from 'react'
4
+ import { useSelectedLanguage } from '../utils/useSelectedLanguage'
5
+
6
+ function CodeSnippets({ children }: { children: React.ReactNode }) {
7
+ const [selectedLang, setSelectedLang] = useSelectedLanguage()
8
+
9
+ const handleOnChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
10
+ setSelectedLang(e.target.value)
11
+ }
12
+
13
+ return (
14
+ <div>
15
+ <form style={{ position: 'relative' }}>
16
+ <select
17
+ name="language"
18
+ id="language"
19
+ onChange={handleOnChange}
20
+ value={selectedLang}
21
+ style={{ position: 'absolute', top: '10px', right: '60px', zIndex: 3 }}
22
+ >
23
+ <option value="js">Javascript</option>
24
+ <option value="ts">Typescript</option>
25
+ </select>
26
+ </form>
27
+ {children}
28
+ </div>
29
+ )
30
+ }
31
+
32
+ function CodeSnippet({
33
+ children,
34
+ language,
35
+ tsOnly = false,
36
+ }: { children: React.ReactNode; language: string; tsOnly: boolean }) {
37
+ const [selectedLang] = useSelectedLanguage()
38
+
39
+ const style = tsOnly ? {} : { display: selectedLang === language ? 'block' : 'none' }
40
+
41
+ const copyToClipboard = async (e: React.MouseEvent<HTMLButtonElement>) => {
42
+ try {
43
+ const figureEl = e.currentTarget.nextElementSibling
44
+ if (figureEl?.tagName === 'FIGURE') {
45
+ await navigator.clipboard.writeText(figureEl.textContent ?? '')
46
+ console.log('Copied to clipboard!')
47
+ }
48
+ } catch (error) {
49
+ console.warn('Copy failed', error)
50
+ }
51
+ }
52
+
53
+ return (
54
+ <div style={{ ...style, position: 'relative' }}>
55
+ <button type="button" style={{ position: 'absolute', top: '10px', right: '10px' }} onClick={copyToClipboard}>
56
+ Copy
57
+ </button>
58
+ {children}
59
+ </div>
60
+ )
61
+ }
62
+
63
+ // Show/hide TypeScript sections (code and/or plain)
64
+ function TypescriptOnly({ children }: { children: React.ReactNode }) {
65
+ const [selectedLang] = useSelectedLanguage()
66
+
67
+ return <div style={{ display: selectedLang === 'ts' ? 'block' : 'none' }}>{children}</div>
68
+ }
@@ -8,3 +8,4 @@ export * from './HorizontalLine'
8
8
  export * from './CodeBlockTransformer'
9
9
  export * from './Comment'
10
10
  export * from './FileRemoved'
11
+ export * from './CodeSnippets'
@@ -0,0 +1,68 @@
1
+ export { detypePlugin }
2
+
3
+ import type { PluginOption } from 'vite'
4
+ import module from 'node:module'
5
+
6
+ // Cannot use `import { transform } from 'detype'` as it results in errors,
7
+ // and the package has no default export. Using `module.createRequire` instead.
8
+ const { transform } = module.createRequire(import.meta.url)('detype') as typeof import('detype')
9
+
10
+ function detypePlugin(): PluginOption {
11
+ return {
12
+ name: '@brillout/docpress:detypePlugin',
13
+ enforce: 'pre',
14
+ transform: async (code: string, id: string) => {
15
+ if (!id.endsWith('+Page.mdx')) {
16
+ return
17
+ }
18
+ const codeNew = await transformCode(code)
19
+ return codeNew
20
+ },
21
+ }
22
+ }
23
+
24
+ const tsBlockRegex = /```(tsx?|vue)(\s+ts-only)?([\s\S]*?)```/g
25
+
26
+ async function transformCode(code: string) {
27
+ let codeNew = `import { CodeSnippets, CodeSnippet } from '@brillout/docpress';\n`
28
+ let lastIndex = 0
29
+
30
+ const matches = [...code.matchAll(tsBlockRegex)]
31
+
32
+ if (matches.length === 0) {
33
+ return code
34
+ }
35
+
36
+ for (const match of matches) {
37
+ const [tsCodeBlock, lang, tsOnly, tsCode] = match // lang = ts | tsx | vue
38
+ const type = lang === 'vue' ? 'vue' : lang.replace('t', 'j') // ts => js | tsx => jsx
39
+
40
+ const blockStart = match.index
41
+ const blockEnd = blockStart + tsCodeBlock.length
42
+
43
+ codeNew += code.slice(lastIndex, blockStart)
44
+
45
+ if (tsOnly) {
46
+ codeNew += `\n<CodeSnippet language={'ts'} tsOnly={'true'}>\n${tsCodeBlock}\n</CodeSnippet>\n`
47
+ } else {
48
+ const jsCode = await transform(tsCode.trim().replaceAll('.ts', '.js'), `tsCode.${lang}`, {
49
+ removeTsComments: true,
50
+ prettierOptions: {
51
+ semi: false,
52
+ singleQuote: true,
53
+ },
54
+ })
55
+
56
+ const jsCodeBlock = `\`\`\`${type}\n${jsCode}\`\`\``
57
+ const jsCodeSnippet = `\n<CodeSnippet language={'js'}>\n${jsCodeBlock}\n</CodeSnippet>\n`
58
+ const tsCodeSnippet = `\n<CodeSnippet language={'ts'}>\n${tsCodeBlock}\n</CodeSnippet>\n`
59
+
60
+ codeNew += `<CodeSnippets>${jsCodeSnippet}${tsCodeSnippet}</CodeSnippets>`
61
+ }
62
+
63
+ lastIndex = blockEnd
64
+ }
65
+ codeNew += code.slice(lastIndex)
66
+
67
+ return codeNew
68
+ }
package/dist/+config.js CHANGED
@@ -1,6 +1,6 @@
1
1
  export { config as default };
2
2
  import { viteConfig } from './vite.config.js';
3
- var config = {
3
+ const config = {
4
4
  name: '@brillout/docpress',
5
5
  require: { vike: '>=0.4.234' },
6
6
  vite: viteConfig,
@@ -1,53 +1,39 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
1
  export { NavItemComponent };
13
2
  export { getNavItemsWithComputed };
14
3
  import React from 'react';
15
4
  import { assert, assertWarning, jsxToTextContent } from './utils/server';
16
5
  import './NavItemComponent.css';
17
6
  import { parseMarkdownMini } from './parseMarkdownMini';
18
- function NavItemComponent(_a) {
19
- var _b;
20
- var _c;
21
- var navItem = _a.navItem, onClick = _a.onClick;
7
+ function NavItemComponent({ navItem, onClick, }) {
22
8
  assert([1, 2, 3, 4].includes(navItem.level), navItem);
23
- var titleJsx = parseMarkdownMini(navItem.title);
24
- var titleInNavJsx = parseMarkdownMini(navItem.titleInNav);
25
- var iconSize = 25;
26
- var icon = navItem.titleIcon && (React.createElement("img", { src: navItem.titleIcon, style: __assign({ height: iconSize, width: iconSize, marginRight: 8, marginLeft: 4 }, navItem.titleIconStyle) }));
9
+ const titleJsx = parseMarkdownMini(navItem.title);
10
+ const titleInNavJsx = parseMarkdownMini(navItem.titleInNav);
11
+ const iconSize = 25;
12
+ const icon = navItem.titleIcon && (React.createElement("img", { src: navItem.titleIcon, style: { height: iconSize, width: iconSize, marginRight: 8, marginLeft: 4, ...navItem.titleIconStyle } }));
27
13
  if (navItem.level === 1 || navItem.level === 4) {
28
14
  assert(navItem.url === undefined);
29
15
  }
30
16
  else {
31
- var sectionTitle = jsxToTextContent(titleJsx);
17
+ const sectionTitle = jsxToTextContent(titleJsx);
32
18
  assertWarning(navItem.url, [
33
- "".concat(jsxToTextContent(titleInNavJsx), " is missing a URL hash."),
34
- "Add a URL hash with: `## ".concat(sectionTitle, "{#some-hash}`."),
19
+ `${jsxToTextContent(titleInNavJsx)} is missing a URL hash.`,
20
+ `Add a URL hash with: \`## ${sectionTitle}{#some-hash}\`.`,
35
21
  /* TO-DO/eventually: not implemented yet.
36
22
  `Use \`<h2 id="url-hash">${sectionTitle}</h2>\` instead of \`## ${sectionTitle}\`.`,
37
23
  */
38
24
  ].join(' '));
39
25
  }
40
- var children = titleInNavJsx;
26
+ let children = titleInNavJsx;
41
27
  if (navItem.level === 1) {
42
28
  children = (React.createElement(React.Fragment, null,
43
29
  icon,
44
30
  children,
45
31
  React.createElement(Chevron, { className: "collapsible-icon", height: 9 })));
46
32
  }
47
- var props = {
48
- href: (_c = navItem.url) !== null && _c !== void 0 ? _c : undefined,
49
- children: children,
50
- onClick: onClick,
33
+ const props = {
34
+ href: navItem.url ?? undefined,
35
+ children,
36
+ onClick,
51
37
  className: [
52
38
  'nav-item',
53
39
  'nav-item-level-' + navItem.level,
@@ -59,45 +45,51 @@ function NavItemComponent(_a) {
59
45
  .join(' '),
60
46
  };
61
47
  if (navItem.level === 1) {
62
- props.style = (_b = {},
63
- _b['--category-color'] = navItem.color,
64
- _b);
48
+ props.style = {
49
+ ['--category-color']: navItem.color,
50
+ };
65
51
  }
66
52
  if (navItem.level === 2 || navItem.level === 3) {
67
- return React.createElement("a", __assign({}, props));
53
+ return React.createElement("a", { ...props });
68
54
  }
69
55
  else {
70
- return React.createElement("span", __assign({}, props));
56
+ return React.createElement("span", { ...props });
71
57
  }
72
58
  }
73
59
  function getNavItemsWithComputed(navItems, currentUrl) {
74
- var navItemIdx;
75
- var navItemsWithComputed = navItems.map(function (navItem, i) {
60
+ let navItemIdx;
61
+ const navItemsWithComputed = navItems.map((navItem, i) => {
76
62
  assert([1, 2, 3, 4].includes(navItem.level), navItem);
77
- var navItemPrevious = navItems[i - 1];
78
- var navItemNext = navItems[i + 1];
79
- var isActive = false;
63
+ const navItemPrevious = navItems[i - 1];
64
+ const navItemNext = navItems[i + 1];
65
+ let isActive = false;
80
66
  if (navItem.url === currentUrl) {
81
- assert(navItem.level === 2, { currentUrl: currentUrl });
67
+ assert(navItem.level === 2, { currentUrl });
82
68
  assert(navItemIdx === undefined);
83
69
  navItemIdx = i;
84
70
  isActive = true;
85
71
  }
86
- var isFirstOfItsKind = navItem.level !== (navItemPrevious === null || navItemPrevious === void 0 ? void 0 : navItemPrevious.level);
87
- var isLastOfItsKind = navItem.level !== (navItemNext === null || navItemNext === void 0 ? void 0 : navItemNext.level);
88
- var navItemComputed = __assign(__assign({}, navItem), { isActive: isActive, isRelevant: false, isFirstOfItsKind: isFirstOfItsKind, isLastOfItsKind: isLastOfItsKind });
72
+ const isFirstOfItsKind = navItem.level !== navItemPrevious?.level;
73
+ const isLastOfItsKind = navItem.level !== navItemNext?.level;
74
+ const navItemComputed = {
75
+ ...navItem,
76
+ isActive,
77
+ isRelevant: false,
78
+ isFirstOfItsKind,
79
+ isLastOfItsKind,
80
+ };
89
81
  return navItemComputed;
90
82
  });
91
83
  // Set `isRelevant`
92
84
  if (navItemIdx !== undefined) {
93
- for (var i = navItemIdx; i >= 0; i--) {
94
- var navItem = navItemsWithComputed[i];
85
+ for (let i = navItemIdx; i >= 0; i--) {
86
+ const navItem = navItemsWithComputed[i];
95
87
  navItem.isRelevant = true;
96
88
  if (navItem.level === 1)
97
89
  break;
98
90
  }
99
- for (var i = navItemIdx; i < navItemsWithComputed.length; i++) {
100
- var navItem = navItemsWithComputed[i];
91
+ for (let i = navItemIdx; i < navItemsWithComputed.length; i++) {
92
+ const navItem = navItemsWithComputed[i];
101
93
  if (navItem.level === 1)
102
94
  break;
103
95
  navItem.isRelevant = true;
@@ -106,6 +98,6 @@ function getNavItemsWithComputed(navItems, currentUrl) {
106
98
  return navItemsWithComputed;
107
99
  }
108
100
  function Chevron(props) {
109
- return (React.createElement("svg", __assign({ viewBox: "0 0 512 292.52", xmlns: "http://www.w3.org/2000/svg" }, props),
101
+ return (React.createElement("svg", { viewBox: "0 0 512 292.52", xmlns: "http://www.w3.org/2000/svg", ...props },
110
102
  React.createElement("path", { fill: "#aaa", d: "M10.725 82.42L230.125 261.82c6.8 6.8 16.2 10.7 25.9 10.7s19.1-3.9 25.9-10.7l219.4-179.4c14.3-14.3 14.3-37.4 0-51.7s-37.4-14.3-51.7 0l-193.6 153.6-193.6-153.6c-14.3-14.3-37.4-14.3-51.7 0s-14.3 37.5 0 51.7z" })));
111
103
  }
@@ -2,9 +2,8 @@ export { CodeBlockTransformer };
2
2
  import React from 'react';
3
3
  import { assert } from '../utils/server';
4
4
  import './CodeBlockTransformer.css';
5
- function CodeBlockTransformer(_a) {
6
- var children = _a.children, lineBreak = _a.lineBreak;
5
+ function CodeBlockTransformer({ children, lineBreak }) {
7
6
  assert(lineBreak === 'white-space' || lineBreak === 'break-word', '`lineBreak` is currently the only use case for <CodeBlockTransformer>');
8
- var className = "with-line-break_".concat(lineBreak);
7
+ const className = `with-line-break_${lineBreak}`;
9
8
  return React.createElement("div", { className: className }, children);
10
9
  }
@@ -0,0 +1,13 @@
1
+ export { CodeSnippets, CodeSnippet, TypescriptOnly };
2
+ import React from 'react';
3
+ declare function CodeSnippets({ children }: {
4
+ children: React.ReactNode;
5
+ }): React.JSX.Element;
6
+ declare function CodeSnippet({ children, language, tsOnly, }: {
7
+ children: React.ReactNode;
8
+ language: string;
9
+ tsOnly: boolean;
10
+ }): React.JSX.Element;
11
+ declare function TypescriptOnly({ children }: {
12
+ children: React.ReactNode;
13
+ }): React.JSX.Element;
@@ -0,0 +1,39 @@
1
+ export { CodeSnippets, CodeSnippet, TypescriptOnly };
2
+ import React from 'react';
3
+ import { useSelectedLanguage } from '../utils/useSelectedLanguage';
4
+ function CodeSnippets({ children }) {
5
+ const [selectedLang, setSelectedLang] = useSelectedLanguage();
6
+ const handleOnChange = (e) => {
7
+ setSelectedLang(e.target.value);
8
+ };
9
+ return (React.createElement("div", null,
10
+ React.createElement("form", { style: { position: 'relative' } },
11
+ React.createElement("select", { name: "language", id: "language", onChange: handleOnChange, value: selectedLang, style: { position: 'absolute', top: '10px', right: '60px', zIndex: 3 } },
12
+ React.createElement("option", { value: "js" }, "Javascript"),
13
+ React.createElement("option", { value: "ts" }, "Typescript"))),
14
+ children));
15
+ }
16
+ function CodeSnippet({ children, language, tsOnly = false, }) {
17
+ const [selectedLang] = useSelectedLanguage();
18
+ const style = tsOnly ? {} : { display: selectedLang === language ? 'block' : 'none' };
19
+ const copyToClipboard = async (e) => {
20
+ try {
21
+ const figureEl = e.currentTarget.nextElementSibling;
22
+ if (figureEl?.tagName === 'FIGURE') {
23
+ await navigator.clipboard.writeText(figureEl.textContent ?? '');
24
+ console.log('Copied to clipboard!');
25
+ }
26
+ }
27
+ catch (error) {
28
+ console.warn('Copy failed', error);
29
+ }
30
+ };
31
+ return (React.createElement("div", { style: { ...style, position: 'relative' } },
32
+ React.createElement("button", { type: "button", style: { position: 'absolute', top: '10px', right: '10px' }, onClick: copyToClipboard }, "Copy"),
33
+ children));
34
+ }
35
+ // Show/hide TypeScript sections (code and/or plain)
36
+ function TypescriptOnly({ children }) {
37
+ const [selectedLang] = useSelectedLanguage();
38
+ return React.createElement("div", { style: { display: selectedLang === 'ts' ? 'block' : 'none' } }, children);
39
+ }
@@ -1,6 +1,5 @@
1
1
  export { Comment };
2
2
  import React from 'react';
3
- function Comment(_a) {
4
- var children = _a.children;
3
+ function Comment({ children }) {
5
4
  return React.createElement(React.Fragment, null);
6
5
  }
@@ -2,25 +2,23 @@ export { FileRemoved };
2
2
  export { FileAdded };
3
3
  import React from 'react';
4
4
  // Styling defined in src/css/code/diff.css
5
- var classRemoved = [
5
+ const classRemoved = [
6
6
  //
7
7
  'diff-entire-file',
8
8
  'diff-entire-file-removed',
9
9
  ].join(' ');
10
- var classAdded = [
10
+ const classAdded = [
11
11
  //
12
12
  'diff-entire-file',
13
13
  'diff-entire-file-added',
14
14
  ].join(' ');
15
- function FileRemoved(_a) {
16
- var children = _a.children;
15
+ function FileRemoved({ children }) {
17
16
  return React.createElement("div", { className: classRemoved },
18
17
  " ",
19
18
  children,
20
19
  " ");
21
20
  }
22
- function FileAdded(_a) {
23
- var children = _a.children;
21
+ function FileAdded({ children }) {
24
22
  return React.createElement("div", { className: classAdded },
25
23
  " ",
26
24
  children,
@@ -1,8 +1,7 @@
1
1
  export { HorizontalLine };
2
2
  import React from 'react';
3
3
  import { cls } from '../utils/cls';
4
- function HorizontalLine(_a) {
5
- var primary = _a.primary;
4
+ function HorizontalLine({ primary }) {
6
5
  return (React.createElement("div", { className: cls(primary && 'primary'), style: { textAlign: 'center' } },
7
6
  React.createElement("hr", { style: {
8
7
  display: 'inline-block',
@@ -1,10 +1,9 @@
1
1
  import React from 'react';
2
2
  import { assert } from '../utils/server';
3
3
  export { ImportMeta };
4
- function ImportMeta(_a) {
5
- var prop = _a.prop;
4
+ function ImportMeta({ prop }) {
6
5
  assert(!prop.startsWith('import'));
7
6
  assert(!prop.startsWith('.'));
8
- var text = 'imp' + 'ort.meta.' + prop;
7
+ const text = 'imp' + 'ort.meta.' + prop;
9
8
  return React.createElement("code", null, text);
10
9
  }
@@ -1,31 +1,22 @@
1
- var __assign = (this && this.__assign) || function () {
2
- __assign = Object.assign || function(t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
- t[p] = s[p];
7
- }
8
- return t;
9
- };
10
- return __assign.apply(this, arguments);
11
- };
12
1
  export { Link };
13
2
  import React from 'react';
14
3
  import { usePageContext } from '../renderer/usePageContext';
15
4
  import { assert, assertUsage, assertWarning, determineSectionTitle, determineSectionUrlHash } from '../utils/server';
16
5
  import { parseMarkdownMini } from '../parseMarkdownMini';
17
6
  import pc from '@brillout/picocolors';
18
- function Link(_a) {
19
- var href = _a.href, text = _a.text, noBreadcrumb = _a.noBreadcrumb, doNotInferSectionTitle = _a.doNotInferSectionTitle, noWarning = _a.noWarning, children = _a.children;
20
- var pageContext = usePageContext();
21
- assertUsage(href.startsWith('/') || href.startsWith('#'), "<Link href /> prop `href==='".concat(href, "'` but should start with '/' or '#'"));
7
+ function Link({ href, text, noBreadcrumb, doNotInferSectionTitle, noWarning, children, }) {
8
+ const pageContext = usePageContext();
9
+ assertUsage(href.startsWith('/') || href.startsWith('#'), `<Link href /> prop \`href==='${href}'\` but should start with '/' or '#'`);
22
10
  assertUsage(!text || !children, 'Cannot use both `text` or `children`');
23
11
  // assertWarning(!text, 'prop `text` is deprecated')
24
- text = text !== null && text !== void 0 ? text : children;
25
- var linkTextData = getLinkTextData({ href: href, pageContext: pageContext, doNotInferSectionTitle: doNotInferSectionTitle, noWarning: noWarning });
12
+ text = text ?? children;
13
+ const linkTextData = getLinkTextData({ href, pageContext, doNotInferSectionTitle, noWarning });
26
14
  if (!text) {
27
15
  if (linkTextData) {
28
- text = getLinkText(__assign({ noBreadcrumb: noBreadcrumb }, linkTextData));
16
+ text = getLinkText({
17
+ noBreadcrumb,
18
+ ...linkTextData,
19
+ });
29
20
  }
30
21
  else {
31
22
  text = 'LINK-TARGET-NOT-FOUND';
@@ -33,12 +24,10 @@ function Link(_a) {
33
24
  }
34
25
  return React.createElement("a", { href: href }, text);
35
26
  }
36
- function getLinkText(_a) {
37
- var _b;
38
- var noBreadcrumb = _a.noBreadcrumb, linkData = _a.linkData, sectionTitle = _a.sectionTitle, isLinkOnSamePage = _a.isLinkOnSamePage;
39
- var breadcrumbParts = [];
27
+ function getLinkText({ noBreadcrumb, linkData, sectionTitle, isLinkOnSamePage, }) {
28
+ const breadcrumbParts = [];
40
29
  if (linkData.linkBreadcrumb) {
41
- breadcrumbParts.push.apply(breadcrumbParts, ((_b = linkData.linkBreadcrumb) !== null && _b !== void 0 ? _b : []).slice().reverse().map(parseMarkdownMini));
30
+ breadcrumbParts.push(...(linkData.linkBreadcrumb ?? []).slice().reverse().map(parseMarkdownMini));
42
31
  }
43
32
  breadcrumbParts.push(parseMarkdownMini(linkData.title));
44
33
  if (sectionTitle)
@@ -46,33 +35,32 @@ function getLinkText(_a) {
46
35
  if (noBreadcrumb || isLinkOnSamePage) {
47
36
  return breadcrumbParts[breadcrumbParts.length - 1];
48
37
  }
49
- return (React.createElement(React.Fragment, null, breadcrumbParts.map(function (title, i) {
50
- var seperator = i === 0 ? React.createElement(React.Fragment, null) : ' > ';
38
+ return (React.createElement(React.Fragment, null, breadcrumbParts.map((title, i) => {
39
+ const seperator = i === 0 ? React.createElement(React.Fragment, null) : ' > ';
51
40
  return (React.createElement(React.Fragment, { key: i },
52
41
  seperator,
53
42
  title));
54
43
  })));
55
44
  }
56
- function getLinkTextData(_a) {
57
- var href = _a.href, pageContext = _a.pageContext, doNotInferSectionTitle = _a.doNotInferSectionTitle, noWarning = _a.noWarning;
58
- var _b = parseHref(href), hrefPathname = _b.hrefPathname, hrefHash = _b.hrefHash;
59
- var linkData = findLinkData(hrefPathname || pageContext.urlPathname, { pageContext: pageContext, noWarning: noWarning });
45
+ function getLinkTextData({ href, pageContext, doNotInferSectionTitle, noWarning, }) {
46
+ const { hrefPathname, hrefHash } = parseHref(href);
47
+ const linkData = findLinkData(hrefPathname || pageContext.urlPathname, { pageContext, noWarning });
60
48
  if (!linkData)
61
49
  return null;
62
- var isLinkOnSamePage = linkData.url === pageContext.urlPathname;
50
+ const isLinkOnSamePage = linkData.url === pageContext.urlPathname;
63
51
  if (!hrefPathname)
64
52
  assert(isLinkOnSamePage);
65
- var sectionTitle = null;
53
+ let sectionTitle = null;
66
54
  if (hrefHash) {
67
55
  assert(!hrefHash.startsWith('#'));
68
56
  if (isLinkOnSamePage) {
69
- var linkDataPageSection = findLinkData("#".concat(hrefHash), { pageContext: pageContext, noWarning: noWarning });
57
+ const linkDataPageSection = findLinkData(`#${hrefHash}`, { pageContext, noWarning });
70
58
  if (!linkDataPageSection)
71
59
  return null;
72
60
  sectionTitle = parseMarkdownMini(linkDataPageSection.title);
73
61
  }
74
62
  else if ('sectionTitles' in linkData && linkData.sectionTitles) {
75
- linkData.sectionTitles.forEach(function (title) {
63
+ linkData.sectionTitles.forEach((title) => {
76
64
  if (determineSectionUrlHash(title) === hrefHash) {
77
65
  sectionTitle = parseMarkdownMini(title);
78
66
  }
@@ -80,32 +68,28 @@ function getLinkTextData(_a) {
80
68
  }
81
69
  if (!sectionTitle) {
82
70
  if (doNotInferSectionTitle) {
83
- assertWarning(false, "Page section title not found for <Link href=\"`".concat(href, "`\" doNotInferSectionTitle={true} />."));
71
+ assertWarning(false, `Page section title not found for <Link href="\`${href}\`" doNotInferSectionTitle={true} />.`);
84
72
  return null;
85
73
  }
86
74
  sectionTitle = React.createElement(React.Fragment, null, determineSectionTitle(href));
87
75
  }
88
76
  }
89
- return { linkData: linkData, sectionTitle: sectionTitle, isLinkOnSamePage: isLinkOnSamePage };
77
+ return { linkData, sectionTitle, isLinkOnSamePage };
90
78
  }
91
- function findLinkData(href, _a) {
92
- var pageContext = _a.pageContext, noWarning = _a.noWarning;
79
+ function findLinkData(href, { pageContext, noWarning }) {
93
80
  assert(href.startsWith('/') || href.startsWith('#'));
94
- var linksAll = pageContext.resolved.linksAll;
95
- var linkData = linksAll.find(function (_a) {
96
- var url = _a.url;
97
- return href === url;
98
- });
81
+ const { linksAll } = pageContext.resolved;
82
+ const linkData = linksAll.find(({ url }) => href === url);
99
83
  if (href.startsWith('#')) {
100
84
  if (!noWarning) {
101
- assertWarning(linkData, "Couldn't find ".concat(href, " in ").concat(pageContext.urlPathname, ", does it exist?"));
85
+ assertWarning(linkData, `Couldn't find ${href} in ${pageContext.urlPathname}, does it exist?`);
102
86
  }
103
87
  }
104
88
  else {
105
89
  if (!noWarning) {
106
90
  assertWarning(linkData, [
107
- "Couldn't find page with URL ".concat(pc.bold(href)),
108
- "\u2014 did you define it in",
91
+ `Couldn't find page with URL ${pc.bold(href)}`,
92
+ `— did you define it in`,
109
93
  [
110
94
  pc.cyan('docpress.config.js'),
111
95
  pc.dim('#{'),
@@ -118,16 +102,16 @@ function findLinkData(href, _a) {
118
102
  ].join(' '));
119
103
  }
120
104
  }
121
- return linkData !== null && linkData !== void 0 ? linkData : null;
105
+ return linkData ?? null;
122
106
  }
123
107
  function parseHref(href) {
124
- var hrefHash = null;
125
- var hrefPathname = null;
108
+ let hrefHash = null;
109
+ let hrefPathname = null;
126
110
  if (!href.includes('#')) {
127
111
  hrefPathname = href;
128
112
  }
129
113
  else {
130
- var _a = href.split('#'), partsFirst = _a[0], partsRest = _a.slice(1);
114
+ const [partsFirst, ...partsRest] = href.split('#');
131
115
  if (partsFirst) {
132
116
  hrefPathname = partsFirst;
133
117
  }
@@ -138,5 +122,5 @@ function parseHref(href) {
138
122
  // Text highlight links e,g. #metadata:~:text=global%20or%20local.-,Global%20metadata,-.
139
123
  if (hrefHash)
140
124
  hrefHash = hrefHash.split(':~:text')[0];
141
- return { hrefPathname: hrefPathname, hrefHash: hrefHash };
125
+ return { hrefPathname, hrefHash };
142
126
  }