@brillout/docpress 0.15.8 → 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 (43) hide show
  1. package/NavItemComponent.tsx +11 -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.d.ts +0 -1
  7. package/dist/NavItemComponent.js +38 -47
  8. package/dist/components/CodeBlockTransformer.js +2 -3
  9. package/dist/components/CodeSnippets.d.ts +13 -0
  10. package/dist/components/CodeSnippets.js +39 -0
  11. package/dist/components/Comment.js +1 -2
  12. package/dist/components/FileRemoved.js +4 -6
  13. package/dist/components/HorizontalLine.js +1 -2
  14. package/dist/components/ImportMeta.js +2 -3
  15. package/dist/components/Link.js +34 -50
  16. package/dist/components/Note.js +17 -29
  17. package/dist/components/P.js +1 -12
  18. package/dist/components/RepoLink.js +7 -9
  19. package/dist/components/index.d.ts +1 -0
  20. package/dist/components/index.js +1 -0
  21. package/dist/determineNavItemsColumnLayout.js +48 -62
  22. package/dist/detypePlugin.d.ts +3 -0
  23. package/dist/detypePlugin.js +53 -0
  24. package/dist/parseMarkdownMini.js +5 -17
  25. package/dist/parsePageSections.js +41 -82
  26. package/dist/renderer/usePageContext.js +6 -7
  27. package/dist/resolvePageContext.js +91 -103
  28. package/dist/utils/Emoji/Emoji.js +13 -21
  29. package/dist/utils/assert.js +14 -16
  30. package/dist/utils/cls.js +1 -1
  31. package/dist/utils/determineSectionUrlHash.js +5 -5
  32. package/dist/utils/filter.js +2 -2
  33. package/dist/utils/getGlobalObject.js +3 -3
  34. package/dist/utils/useSelectedLanguage.d.ts +7 -0
  35. package/dist/utils/useSelectedLanguage.js +48 -0
  36. package/dist/vite.config.js +9 -7
  37. package/global.d.ts +3 -1
  38. package/index.ts +3 -0
  39. package/package.json +2 -1
  40. package/tsconfig.config.json +1 -1
  41. package/tsconfig.json +1 -0
  42. package/utils/useSelectedLanguage.ts +61 -0
  43. package/vite.config.ts +2 -0
@@ -1,23 +1,3 @@
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
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
13
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
14
- if (ar || !(i in from)) {
15
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
16
- ar[i] = from[i];
17
- }
18
- }
19
- return to.concat(ar || Array.prototype.slice.call(from));
20
- };
21
1
  export { resolvePageContext };
22
2
  import { assert } from './utils/assert';
23
3
  import { jsxToTextContent } from './utils/jsxToTextContent';
@@ -25,51 +5,53 @@ import pc from '@brillout/picocolors';
25
5
  import { parseMarkdownMini } from './parseMarkdownMini';
26
6
  import { determineNavItemsColumnLayout } from './determineNavItemsColumnLayout';
27
7
  function resolvePageContext(pageContext) {
28
- var _a;
29
- var config = pageContext.globalContext.config.docpress;
30
- var urlPathname = pageContext.urlPathname;
31
- var pageSections = (_a = pageContext.config.pageSectionsExport) !== null && _a !== void 0 ? _a : [];
8
+ const config = pageContext.globalContext.config.docpress;
9
+ const { urlPathname } = pageContext;
10
+ const pageSections = pageContext.config.pageSectionsExport ?? [];
32
11
  {
33
- var headings = config.headings, headingsDetached = config.headingsDetached;
34
- assertHeadingsDefinition(__spreadArray(__spreadArray([], headings, true), headingsDetached, true));
12
+ const { headings, headingsDetached } = config;
13
+ assertHeadingsDefinition([...headings, ...headingsDetached]);
35
14
  }
36
- var ret = getHeadingsResolved(config);
37
- var headingsDetachedResolved = ret.headingsDetachedResolved;
38
- var headingsResolved = ret.headingsResolved;
39
- var _b = getActiveHeading(headingsResolved, headingsDetachedResolved, urlPathname), activeHeading = _b.activeHeading, isDetachedPage = _b.isDetachedPage, activeCategoryName = _b.activeCategoryName;
40
- var _c = getTitles(activeHeading, urlPathname, config), documentTitle = _c.documentTitle, isLandingPage = _c.isLandingPage, pageTitle = _c.pageTitle;
41
- var pageSectionsResolved = getPageSectionsResolved(pageSections, activeHeading);
42
- var linksGlobal = __spreadArray(__spreadArray([], headingsResolved.map(headingToLinkData), true), headingsDetachedResolved.map(headingToLinkData), true);
43
- var linksPage = pageSectionsResolved.map(pageSectionToLinkData);
44
- var linksAll = __spreadArray(__spreadArray([], linksPage, true), linksGlobal, true);
45
- var navItemsAll;
46
- var navItemsDetached;
15
+ const ret = getHeadingsResolved(config);
16
+ const { headingsDetachedResolved } = ret;
17
+ let { headingsResolved } = ret;
18
+ const { activeHeading, isDetachedPage, activeCategoryName } = getActiveHeading(headingsResolved, headingsDetachedResolved, urlPathname);
19
+ const { documentTitle, isLandingPage, pageTitle } = getTitles(activeHeading, urlPathname, config);
20
+ const pageSectionsResolved = getPageSectionsResolved(pageSections, activeHeading);
21
+ const linksGlobal = [
22
+ ...headingsResolved.map(headingToLinkData),
23
+ ...headingsDetachedResolved.map(headingToLinkData),
24
+ ];
25
+ const linksPage = pageSectionsResolved.map(pageSectionToLinkData);
26
+ const linksAll = [...linksPage, ...linksGlobal];
27
+ let navItemsAll;
28
+ let navItemsDetached;
47
29
  {
48
- var navItemsPageSections = pageSectionsResolved
49
- .filter(function (pageSection) { return pageSection.pageSectionLevel === 2; })
30
+ const navItemsPageSections = pageSectionsResolved
31
+ .filter((pageSection) => pageSection.pageSectionLevel === 2)
50
32
  .map(pageSectionToNavItem);
51
33
  navItemsAll = headingsResolved.map(headingToNavItem);
52
34
  determineNavItemsColumnLayout(navItemsAll);
53
35
  if (isDetachedPage) {
54
- navItemsDetached = __spreadArray([headingToNavItem(activeHeading)], navItemsPageSections, true);
36
+ navItemsDetached = [headingToNavItem(activeHeading), ...navItemsPageSections];
55
37
  }
56
38
  else {
57
- var activeHeadingIndex_1 = navItemsAll.findIndex(function (navItem) { return navItem.url === urlPathname; });
58
- assert(activeHeadingIndex_1 >= 0);
59
- navItemsPageSections.forEach(function (navItem, i) {
60
- navItemsAll.splice(activeHeadingIndex_1 + 1 + i, 0, navItem);
39
+ const activeHeadingIndex = navItemsAll.findIndex((navItem) => navItem.url === urlPathname);
40
+ assert(activeHeadingIndex >= 0);
41
+ navItemsPageSections.forEach((navItem, i) => {
42
+ navItemsAll.splice(activeHeadingIndex + 1 + i, 0, navItem);
61
43
  });
62
44
  }
63
45
  }
64
- var resolved = {
65
- navItemsAll: navItemsAll,
66
- navItemsDetached: navItemsDetached,
46
+ const resolved = {
47
+ navItemsAll,
48
+ navItemsDetached,
67
49
  pageDesign: activeHeading.pageDesign,
68
- linksAll: linksAll,
69
- isLandingPage: isLandingPage,
70
- pageTitle: pageTitle,
71
- documentTitle: documentTitle,
72
- activeCategoryName: activeCategoryName,
50
+ linksAll,
51
+ isLandingPage,
52
+ pageTitle,
53
+ documentTitle,
54
+ activeCategoryName,
73
55
  };
74
56
  return resolved;
75
57
  }
@@ -109,26 +91,24 @@ function pageSectionToLinkData(pageSection) {
109
91
  };
110
92
  }
111
93
  function getTitles(activeHeading, urlPathname, config) {
112
- var isLandingPage = urlPathname === '/';
113
- var title = activeHeading.title;
114
- var pageTitle = isLandingPage ? null : title;
115
- var documentTitle = activeHeading.titleDocument || jsxToTextContent(parseMarkdownMini(title));
94
+ const isLandingPage = urlPathname === '/';
95
+ const { title } = activeHeading;
96
+ let pageTitle = isLandingPage ? null : title;
97
+ let documentTitle = activeHeading.titleDocument || jsxToTextContent(parseMarkdownMini(title));
116
98
  if (!isLandingPage) {
117
99
  documentTitle += ' | ' + config.name;
118
100
  }
119
101
  if (isLandingPage) {
120
102
  pageTitle = null;
121
103
  }
122
- return { documentTitle: documentTitle, isLandingPage: isLandingPage, pageTitle: pageTitle };
104
+ return { documentTitle, isLandingPage, pageTitle };
123
105
  }
124
106
  function getActiveHeading(headingsResolved, headingsDetachedResolved, urlPathname) {
125
- var _a;
126
- var activeHeading = null;
127
- var activeCategoryName = 'Miscellaneous';
128
- var headingCategory;
107
+ let activeHeading = null;
108
+ let activeCategoryName = 'Miscellaneous';
109
+ let headingCategory;
129
110
  assert(urlPathname);
130
- for (var _i = 0, headingsResolved_1 = headingsResolved; _i < headingsResolved_1.length; _i++) {
131
- var heading = headingsResolved_1[_i];
111
+ for (const heading of headingsResolved) {
132
112
  if (heading.level === 1) {
133
113
  headingCategory = heading.title;
134
114
  }
@@ -136,79 +116,87 @@ function getActiveHeading(headingsResolved, headingsDetachedResolved, urlPathnam
136
116
  activeHeading = heading;
137
117
  assert(headingCategory);
138
118
  activeCategoryName = headingCategory;
139
- assert(heading.level === 2, { pageUrl: urlPathname, heading: heading });
119
+ assert(heading.level === 2, { pageUrl: urlPathname, heading });
140
120
  break;
141
121
  }
142
122
  }
143
- var isDetachedPage = !activeHeading;
123
+ const isDetachedPage = !activeHeading;
144
124
  if (!activeHeading) {
145
- activeHeading = (_a = headingsDetachedResolved.find(function (_a) {
146
- var url = _a.url;
147
- return urlPathname === url;
148
- })) !== null && _a !== void 0 ? _a : null;
125
+ activeHeading = headingsDetachedResolved.find(({ url }) => urlPathname === url) ?? null;
149
126
  }
150
127
  if (!activeHeading) {
151
128
  throw new Error([
152
- "URL ".concat(pc.bold(urlPathname), " not found in following URLs:"),
153
- __spreadArray(__spreadArray([], headingsResolved, true), headingsDetachedResolved, true).filter(Boolean)
154
- .map(function (h) { return h.url; })
129
+ `URL ${pc.bold(urlPathname)} not found in following URLs:`,
130
+ [...headingsResolved, ...headingsDetachedResolved]
131
+ .filter(Boolean)
132
+ .map((h) => h.url)
155
133
  .sort()
156
- .map(function (url) { return " ".concat(url); })
134
+ .map((url) => ` ${url}`)
157
135
  .join('\n'),
158
136
  ].join('\n'));
159
137
  }
160
138
  if (activeHeading.category)
161
139
  activeCategoryName = activeHeading.category;
162
- return { activeHeading: activeHeading, isDetachedPage: isDetachedPage, activeCategoryName: activeCategoryName };
140
+ return { activeHeading, isDetachedPage, activeCategoryName };
163
141
  }
164
142
  function getPageSectionsResolved(pageSections, activeHeading) {
165
- var pageSectionsResolved = pageSections.map(function (pageSection) {
166
- var _a;
167
- var pageSectionTitle = pageSection.pageSectionTitle;
168
- var url = pageSection.pageSectionId === null ? null : '#' + pageSection.pageSectionId;
169
- var pageSectionResolved = {
170
- url: url,
143
+ const pageSectionsResolved = pageSections.map((pageSection) => {
144
+ const { pageSectionTitle } = pageSection;
145
+ const url = pageSection.pageSectionId === null ? null : '#' + pageSection.pageSectionId;
146
+ const pageSectionResolved = {
147
+ url,
171
148
  title: pageSectionTitle,
172
- linkBreadcrumb: __spreadArray([activeHeading.title], ((_a = activeHeading.linkBreadcrumb) !== null && _a !== void 0 ? _a : []), true),
149
+ linkBreadcrumb: [activeHeading.title, ...(activeHeading.linkBreadcrumb ?? [])],
173
150
  titleInNav: pageSectionTitle,
174
151
  pageSectionLevel: pageSection.pageSectionLevel,
175
152
  };
176
153
  return pageSectionResolved;
177
154
  });
178
- if (activeHeading === null || activeHeading === void 0 ? void 0 : activeHeading.sectionTitles) {
179
- activeHeading.sectionTitles.forEach(function (sectionTitle) {
180
- var pageSectionTitles = pageSections.map(function (h) { return h.pageSectionTitle; });
181
- assert(pageSectionTitles.includes(sectionTitle), { pageHeadingTitles: pageSectionTitles, sectionTitle: sectionTitle });
155
+ if (activeHeading?.sectionTitles) {
156
+ activeHeading.sectionTitles.forEach((sectionTitle) => {
157
+ const pageSectionTitles = pageSections.map((h) => h.pageSectionTitle);
158
+ assert(pageSectionTitles.includes(sectionTitle), { pageHeadingTitles: pageSectionTitles, sectionTitle });
182
159
  });
183
160
  }
184
161
  return pageSectionsResolved;
185
162
  }
186
163
  function getHeadingsResolved(config) {
187
- var headingsWithoutBreadcrumb = config.headings.map(function (heading) {
188
- var titleInNav = heading.titleInNav || heading.title;
189
- var headingResolved = __assign(__assign({}, heading), { titleInNav: titleInNav });
164
+ const headingsWithoutBreadcrumb = config.headings.map((heading) => {
165
+ const titleInNav = heading.titleInNav || heading.title;
166
+ const headingResolved = {
167
+ ...heading,
168
+ titleInNav,
169
+ };
190
170
  return headingResolved;
191
171
  });
192
- var headingsResolved = [];
193
- headingsWithoutBreadcrumb.forEach(function (heading) {
194
- var linkBreadcrumb = getHeadingsBreadcrumb(heading, headingsResolved);
195
- headingsResolved.push(__assign(__assign({}, heading), { linkBreadcrumb: linkBreadcrumb }));
172
+ const headingsResolved = [];
173
+ headingsWithoutBreadcrumb.forEach((heading) => {
174
+ const linkBreadcrumb = getHeadingsBreadcrumb(heading, headingsResolved);
175
+ headingsResolved.push({
176
+ ...heading,
177
+ linkBreadcrumb,
178
+ });
196
179
  });
197
- var headingsDetachedResolved = config.headingsDetached.map(function (headingsDetached) {
198
- var url = headingsDetached.url;
199
- assert(headingsResolved.find(function (heading) { return heading.url === url; }) === undefined, "remove ".concat(headingsDetached.url, " from headingsDetached"));
200
- return __assign(__assign({}, headingsDetached), { level: 2, titleInNav: headingsDetached.title, linkBreadcrumb: null });
180
+ const headingsDetachedResolved = config.headingsDetached.map((headingsDetached) => {
181
+ const { url } = headingsDetached;
182
+ assert(headingsResolved.find((heading) => heading.url === url) === undefined, `remove ${headingsDetached.url} from headingsDetached`);
183
+ return {
184
+ ...headingsDetached,
185
+ level: 2,
186
+ titleInNav: headingsDetached.title,
187
+ linkBreadcrumb: null,
188
+ };
201
189
  });
202
- return { headingsResolved: headingsResolved, headingsDetachedResolved: headingsDetachedResolved };
190
+ return { headingsResolved, headingsDetachedResolved };
203
191
  }
204
192
  function getHeadingsBreadcrumb(heading, headings) {
205
- var linkBreadcrumb = [];
206
- var levelCurrent = heading.level;
193
+ const linkBreadcrumb = [];
194
+ let levelCurrent = heading.level;
207
195
  headings
208
196
  .slice()
209
197
  .reverse()
210
- .forEach(function (parentCandidate) {
211
- var isParent = parentCandidate.level < levelCurrent;
198
+ .forEach((parentCandidate) => {
199
+ const isParent = parentCandidate.level < levelCurrent;
212
200
  if (isParent) {
213
201
  levelCurrent = parentCandidate.level;
214
202
  linkBreadcrumb.push(parentCandidate.title);
@@ -217,9 +205,9 @@ function getHeadingsBreadcrumb(heading, headings) {
217
205
  return linkBreadcrumb;
218
206
  }
219
207
  function assertHeadingsDefinition(headings) {
220
- headings.forEach(function (heading) {
208
+ headings.forEach((heading) => {
221
209
  if (heading.url) {
222
- var url = heading.url;
210
+ const { url } = heading;
223
211
  assert(url.startsWith('/'));
224
212
  }
225
213
  });
@@ -1,22 +1,10 @@
1
1
  // Let's eventually remove this. Telefunc is still using this as of 2024-12.
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  import React from 'react';
14
3
  import { assert } from '../assert';
15
4
  import { iconMechanicalArm, iconShield, iconTypescript, iconEngine } from './assets';
16
5
  export { Emoji };
17
- function Emoji(_a) {
18
- var name = _a.name, style = _a.style;
19
- var emoji =
6
+ function Emoji({ name, style }) {
7
+ const emoji =
20
8
  // ***
21
9
  // U+26A0
22
10
  // https://emojipedia.org/warning/
@@ -165,22 +153,26 @@ function Emoji(_a) {
165
153
  (name === 'left-right-arrow' && Unicode(0xFE0F)) ||
166
154
  (name === 'left-right-arrow' && Unicode(0xFE0F, { fontFamily: 'reset' })) ||
167
155
  ======================== */
168
- assert(emoji, { name: name });
156
+ assert(emoji, { name });
169
157
  return emoji;
170
158
  function Unicode(codePoint, styleAddendum) {
171
- var text = String.fromCodePoint(codePoint);
159
+ const text = String.fromCodePoint(codePoint);
172
160
  if (style || styleAddendum) {
173
- return React.createElement('span', { style: __assign(__assign({}, style), styleAddendum) }, text);
161
+ return React.createElement('span', { style: { ...style, ...styleAddendum } }, text);
174
162
  }
175
163
  else {
176
164
  return React.createElement(React.Fragment, null, text);
177
165
  }
178
166
  }
179
- function Img(imgSrc, width) {
180
- if (width === void 0) { width = '1.15em'; }
181
- var props = {
167
+ function Img(imgSrc, width = '1.15em') {
168
+ const props = {
182
169
  src: imgSrc,
183
- style: __assign({ verticalAlign: 'text-top', fontSize: '1em', width: width }, style),
170
+ style: {
171
+ verticalAlign: 'text-top',
172
+ fontSize: '1em',
173
+ width,
174
+ ...style,
175
+ },
184
176
  };
185
177
  return React.createElement('img', props);
186
178
  }
@@ -2,8 +2,8 @@ export { assert };
2
2
  export { assertUsage };
3
3
  export { assertWarning };
4
4
  import { getGlobalObject } from './getGlobalObject.js';
5
- var devModeKey = '__docpress_dev_mode';
6
- var globalObject = getGlobalObject('utils/assert.ts', {
5
+ const devModeKey = '__docpress_dev_mode';
6
+ const globalObject = getGlobalObject('utils/assert.ts', {
7
7
  alreadyLogged: new Set(),
8
8
  });
9
9
  if (isBrowser()) {
@@ -18,7 +18,7 @@ if (isBrowser()) {
18
18
  .join(' '));
19
19
  }
20
20
  if (isDevMode()) {
21
- window.onerror = function (err) {
21
+ window.onerror = (err) => {
22
22
  window.alert(err);
23
23
  window.onerror = null;
24
24
  };
@@ -27,25 +27,25 @@ function assert(condition, debugInfo) {
27
27
  if (condition) {
28
28
  return;
29
29
  }
30
- var hasDebugInfo = debugInfo !== undefined;
30
+ const hasDebugInfo = debugInfo !== undefined;
31
31
  if (hasDebugInfo) {
32
32
  console.log(debugInfo);
33
33
  if (typeof debugInfo === 'object') {
34
34
  debugInfo = JSON.stringify(debugInfo);
35
35
  }
36
36
  }
37
- var errMsg = '[DocPress] Bug. Contact DocPress maintainer.';
37
+ let errMsg = '[DocPress] Bug. Contact DocPress maintainer.';
38
38
  if (hasDebugInfo) {
39
39
  errMsg += ' Debug info: ' + String(debugInfo);
40
40
  }
41
- var err = new Error(errMsg);
41
+ const err = new Error(errMsg);
42
42
  throw err;
43
43
  }
44
44
  function assertUsage(condition, msg) {
45
45
  if (condition) {
46
46
  return;
47
47
  }
48
- var err = new Error('[DocPress][Wrong Usage] ' + msg);
48
+ const err = new Error('[DocPress][Wrong Usage] ' + msg);
49
49
  throw err;
50
50
  }
51
51
  function isBrowser() {
@@ -55,34 +55,32 @@ function isDevMode() {
55
55
  return isBrowser() && (!!window.localStorage[devModeKey] || isLocalhost());
56
56
  }
57
57
  function isLocalhost() {
58
- var _a;
59
- return ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.port) !== '';
58
+ return window?.location?.port !== '';
60
59
  }
61
60
  function toggleDevMode() {
62
61
  if (isLocalhost())
63
62
  throw new Error('On localhost DEV MODE is always on.');
64
- var isEnabled = function () { return window.localStorage[devModeKey]; };
63
+ const isEnabled = () => window.localStorage[devModeKey];
65
64
  if (!isEnabled()) {
66
65
  window.localStorage[devModeKey] = 'true';
67
66
  }
68
67
  else {
69
68
  delete window.localStorage[devModeKey];
70
69
  }
71
- console.log("DEV MODE ".concat(isEnabled() ? 'enabled' : 'disabled'));
70
+ console.log(`DEV MODE ${isEnabled() ? 'enabled' : 'disabled'}`);
72
71
  }
73
- function assertWarning(condition, msg, _a) {
74
- var _b = _a === void 0 ? {} : _a, _c = _b.onlyOnce, onlyOnce = _c === void 0 ? true : _c, showStackTrace = _b.showStackTrace;
72
+ function assertWarning(condition, msg, { onlyOnce = true, showStackTrace } = {}) {
75
73
  if (condition) {
76
74
  return;
77
75
  }
78
- var err = new Error(msg);
76
+ const err = new Error(msg);
79
77
  if (!import.meta.env.DEV) {
80
78
  throw err;
81
79
  }
82
80
  else {
83
81
  if (onlyOnce) {
84
- var alreadyLogged = globalObject.alreadyLogged;
85
- var key = onlyOnce === true ? msg : onlyOnce;
82
+ const { alreadyLogged } = globalObject;
83
+ const key = onlyOnce === true ? msg : onlyOnce;
86
84
  if (alreadyLogged.has(key))
87
85
  return;
88
86
  alreadyLogged.add(key);
package/dist/utils/cls.js CHANGED
@@ -1,5 +1,5 @@
1
1
  export { cls };
2
2
  function cls(className) {
3
- var classNames = Array.isArray(className) ? className : [className];
3
+ const classNames = Array.isArray(className) ? className : [className];
4
4
  return classNames.filter(Boolean).join(' ');
5
5
  }
@@ -4,7 +4,7 @@ export { determineSectionTitle };
4
4
  function determineSectionUrlHash(title) {
5
5
  title = title.toLowerCase();
6
6
  title = removeAccentsAndDiacritics(title);
7
- var urlHash = title
7
+ const urlHash = title
8
8
  // \u4E00-\u9FA5 are chinese characters, see https://github.com/brillout/docpress/pull/2
9
9
  .split(/[^a-z0-9\u4E00-\u9FA5]+/)
10
10
  .filter(Boolean)
@@ -20,11 +20,11 @@ function removeAccentsAndDiacritics(str) {
20
20
  return str.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
21
21
  }
22
22
  function determineSectionTitle(urlWithHash) {
23
- assert(urlWithHash.includes('#'), { urlWithHash: urlWithHash });
24
- var urlHash = urlWithHash.split('#')[1];
25
- var title = urlHash
23
+ assert(urlWithHash.includes('#'), { urlWithHash });
24
+ const urlHash = urlWithHash.split('#')[1];
25
+ const title = urlHash
26
26
  .split('-')
27
- .map(function (word, i) {
27
+ .map((word, i) => {
28
28
  if (i === 0) {
29
29
  return capitalizeFirstLetter(word);
30
30
  }
@@ -1,8 +1,8 @@
1
1
  export { filter };
2
2
  // https://stackoverflow.com/questions/66341757/typescript-how-to-filter-the-object
3
3
  function filter(obj, predicate) {
4
- var result = {};
5
- Object.keys(obj).forEach(function (name) {
4
+ const result = {};
5
+ Object.keys(obj).forEach((name) => {
6
6
  if (predicate(obj[name], name)) {
7
7
  result[name] = obj[name];
8
8
  }
@@ -2,8 +2,8 @@ export function getGlobalObject(
2
2
  // We use the filename as key; each `getGlobalObject()` call should live inside a file with a unique filename.
3
3
  key, defaultValue) {
4
4
  // @ts-ignore
5
- var globalObjectsAll = (globalThis[projectKey] = globalThis[projectKey] || {});
6
- var globalObject = (globalObjectsAll[key] = globalObjectsAll[key] || defaultValue);
5
+ const globalObjectsAll = (globalThis[projectKey] = globalThis[projectKey] || {});
6
+ const globalObject = (globalObjectsAll[key] = globalObjectsAll[key] || defaultValue);
7
7
  return globalObject;
8
8
  }
9
- var projectKey = '_docpress';
9
+ const projectKey = '_docpress';
@@ -0,0 +1,7 @@
1
+ export { useSelectedLanguage };
2
+ declare global {
3
+ interface WindowEventMap {
4
+ 'lang-storage': CustomEvent;
5
+ }
6
+ }
7
+ declare function useSelectedLanguage(): readonly [string, (value: string) => void];
@@ -0,0 +1,48 @@
1
+ export { useSelectedLanguage };
2
+ import { useState, useEffect, useCallback } from 'react';
3
+ const key = 'docpress:selectedLang';
4
+ const defaultSsrLang = 'ts';
5
+ const defaultClientLang = 'js';
6
+ function useSelectedLanguage() {
7
+ const [selectedLang, setSelectedLang] = useState(defaultSsrLang);
8
+ const getValue = () => {
9
+ try {
10
+ return localStorage.getItem(key) ?? defaultClientLang;
11
+ }
12
+ catch (error) {
13
+ console.warn('Error reading from localStorage:', error);
14
+ return defaultClientLang;
15
+ }
16
+ };
17
+ const setValue = useCallback((value) => {
18
+ try {
19
+ window.localStorage.setItem(key, value);
20
+ setSelectedLang(value);
21
+ window.dispatchEvent(new CustomEvent('lang-storage'));
22
+ }
23
+ catch (error) {
24
+ console.warn('Error setting localStorage:', error);
25
+ }
26
+ }, []);
27
+ useEffect(() => {
28
+ // Initial load from localStorage
29
+ setSelectedLang(getValue());
30
+ // Update language in current tab
31
+ const handleCustomEvent = () => {
32
+ setSelectedLang(getValue());
33
+ };
34
+ // Update language if changed in another tab
35
+ const handleNativeStorage = (event) => {
36
+ if (event.key === key) {
37
+ setSelectedLang(getValue());
38
+ }
39
+ };
40
+ window.addEventListener('lang-storage', handleCustomEvent);
41
+ window.addEventListener('storage', handleNativeStorage);
42
+ return () => {
43
+ window.removeEventListener('lang-storage', handleCustomEvent);
44
+ window.removeEventListener('storage', handleNativeStorage);
45
+ };
46
+ }, []);
47
+ return [selectedLang, setValue];
48
+ }
@@ -2,18 +2,20 @@ export { config as viteConfig };
2
2
  import mdx from '@mdx-js/rollup';
3
3
  import react from '@vitejs/plugin-react-swc';
4
4
  import { parsePageSections } from './parsePageSections.js';
5
+ import { detypePlugin } from './detypePlugin.js';
5
6
  import rehypePrettyCode from 'rehype-pretty-code';
6
7
  import remarkGfm from 'remark-gfm';
7
8
  import { transformerNotationDiff } from '@shikijs/transformers';
8
- var root = process.cwd();
9
- var prettyCode = [rehypePrettyCode, { theme: 'github-light', transformers: [transformerNotationDiff()] }];
10
- var rehypePlugins = [prettyCode];
11
- var remarkPlugins = [remarkGfm];
12
- var config = {
13
- root: root,
9
+ const root = process.cwd();
10
+ const prettyCode = [rehypePrettyCode, { theme: 'github-light', transformers: [transformerNotationDiff()] }];
11
+ const rehypePlugins = [prettyCode];
12
+ const remarkPlugins = [remarkGfm];
13
+ const config = {
14
+ root,
14
15
  plugins: [
15
16
  parsePageSections(),
16
- mdx({ rehypePlugins: rehypePlugins, remarkPlugins: remarkPlugins }),
17
+ detypePlugin(),
18
+ mdx({ rehypePlugins, remarkPlugins }),
17
19
  // @vitejs/plugin-react-swc needs to be added *after* the mdx plugins
18
20
  react(),
19
21
  ],
package/global.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  // https://stackoverflow.com/questions/52005083/how-to-define-css-variables-in-style-attribute-in-react-and-typescript/70398145#70398145
2
2
  import 'react'
3
- module 'react' {
3
+ declare module 'react' {
4
4
  interface CSSProperties {
5
5
  [key: `--${string}`]: string | number | undefined
6
6
  }
7
7
  }
8
+ // The only purpose of `FakeExport` is to be able to use `import type`
9
+ export type FakeExport = never
package/index.ts CHANGED
@@ -6,6 +6,9 @@ export {
6
6
  FileRemoved,
7
7
  ImportMeta,
8
8
  Emoji,
9
+ CodeSnippets,
10
+ CodeSnippet,
11
+ TypescriptOnly,
9
12
  } from './components'
10
13
  export { MenuToggle } from './Layout'
11
14
 
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@brillout/docpress",
3
- "version": "0.15.8",
3
+ "version": "0.15.10-commit-ef0b9a0",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "@brillout/picocolors": "^1.0.10",
7
+ "detype": "^1.1.2",
7
8
  "@docsearch/css": "3.9.0",
8
9
  "@docsearch/react": "3.9.0",
9
10
  "@mdx-js/mdx": "3.0.1",
@@ -1,4 +1,4 @@
1
1
  {
2
2
  "extends": "./tsconfig.json",
3
- "files": ["vite.config.ts", "+config.ts"]
3
+ "files": ["vite.config.ts", "+config.ts", "global.d.ts"]
4
4
  }
package/tsconfig.json CHANGED
@@ -3,6 +3,7 @@
3
3
  "outDir": "./dist/",
4
4
  "jsx": "react",
5
5
  "module": "ES2020",
6
+ "target": "ES2020",
6
7
  "moduleResolution": "Bundler",
7
8
  "lib": ["DOM", "DOM.Iterable", "ESNext"],
8
9
  "types": ["vite/client"],