@brillout/docpress 0.15.10 → 0.15.11-commit-e615832

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 (48) hide show
  1. package/Layout.tsx +3 -0
  2. package/autoScrollNav.ts +3 -3
  3. package/code-blocks/components/CodeSnippets.css +74 -0
  4. package/code-blocks/components/CodeSnippets.tsx +51 -0
  5. package/code-blocks/components/Pre.css +51 -0
  6. package/code-blocks/components/Pre.tsx +70 -0
  7. package/code-blocks/hooks/useMDXComponents.tsx +13 -0
  8. package/code-blocks/hooks/useSelectCodeLang.ts +74 -0
  9. package/code-blocks/rehypeMetaToProps.ts +69 -0
  10. package/code-blocks/remarkDetype.ts +192 -0
  11. package/code-blocks/shikiTransformerAutoLinks.ts +61 -0
  12. package/css/button.css +23 -0
  13. package/css/code.css +3 -21
  14. package/css/tooltip.css +10 -2
  15. package/dist/+config.js +1 -1
  16. package/dist/NavItemComponent.js +38 -46
  17. package/dist/code-blocks/rehypeMetaToProps.d.ts +35 -0
  18. package/dist/code-blocks/rehypeMetaToProps.js +62 -0
  19. package/dist/code-blocks/remarkDetype.d.ts +4 -0
  20. package/dist/code-blocks/remarkDetype.js +164 -0
  21. package/dist/code-blocks/shikiTransformerAutoLinks.d.ts +8 -0
  22. package/dist/code-blocks/shikiTransformerAutoLinks.js +51 -0
  23. package/dist/components/CodeBlockTransformer.js +2 -3
  24. package/dist/components/Comment.js +1 -2
  25. package/dist/components/FileRemoved.js +4 -6
  26. package/dist/components/HorizontalLine.js +1 -2
  27. package/dist/components/ImportMeta.js +2 -3
  28. package/dist/components/Link.js +34 -50
  29. package/dist/components/Note.js +17 -29
  30. package/dist/components/P.js +1 -12
  31. package/dist/components/RepoLink.js +7 -9
  32. package/dist/determineNavItemsColumnLayout.js +48 -63
  33. package/dist/parseMarkdownMini.js +5 -17
  34. package/dist/parsePageSections.js +41 -82
  35. package/dist/renderer/usePageContext.js +6 -7
  36. package/dist/resolvePageContext.js +103 -110
  37. package/dist/utils/Emoji/Emoji.js +13 -21
  38. package/dist/utils/assert.js +14 -16
  39. package/dist/utils/cls.js +1 -1
  40. package/dist/utils/determineSectionUrlHash.js +5 -5
  41. package/dist/utils/filter.js +2 -2
  42. package/dist/utils/getGlobalObject.js +3 -3
  43. package/dist/vite.config.js +17 -7
  44. package/index.ts +16 -14
  45. package/package.json +7 -2
  46. package/resolvePageContext.ts +19 -15
  47. package/tsconfig.json +2 -1
  48. package/vite.config.ts +14 -4
@@ -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';
@@ -5,15 +5,25 @@ import { parsePageSections } from './parsePageSections.js';
5
5
  import rehypePrettyCode from 'rehype-pretty-code';
6
6
  import remarkGfm from 'remark-gfm';
7
7
  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,
8
+ import { rehypeMetaToProps } from './code-blocks/rehypeMetaToProps.js';
9
+ import { remarkDetype } from './code-blocks/remarkDetype.js';
10
+ import { shikiTransformerAutoLinks } from './code-blocks/shikiTransformerAutoLinks.js';
11
+ const root = process.cwd();
12
+ const prettyCode = [
13
+ rehypePrettyCode,
14
+ {
15
+ theme: 'github-light',
16
+ keepBackground: false,
17
+ transformers: [transformerNotationDiff(), shikiTransformerAutoLinks()],
18
+ },
19
+ ];
20
+ const rehypePlugins = [prettyCode, [rehypeMetaToProps]];
21
+ const remarkPlugins = [remarkGfm, remarkDetype];
22
+ const config = {
23
+ root,
14
24
  plugins: [
15
25
  parsePageSections(),
16
- mdx({ rehypePlugins: rehypePlugins, remarkPlugins: remarkPlugins }),
26
+ mdx({ rehypePlugins, remarkPlugins, providerImportSource: '@brillout/docpress' }),
17
27
  // @vitejs/plugin-react-swc needs to be added *after* the mdx plugins
18
28
  react(),
19
29
  ],
package/index.ts CHANGED
@@ -1,21 +1,23 @@
1
- export {
2
- CodeBlockTransformer,
3
- Link,
4
- RepoLink,
5
- FileAdded,
6
- FileRemoved,
7
- ImportMeta,
8
- Emoji,
9
- } from './components'
1
+ /**********/
2
+ /* PUBLIC */
3
+ /**********/
4
+ export { CodeBlockTransformer, Link, RepoLink, FileAdded, FileRemoved, ImportMeta, Emoji } from './components'
5
+ export { TypescriptOnly } from './code-blocks/components/CodeSnippets'
10
6
  export { MenuToggle } from './Layout'
11
-
12
- // The only place usePageContext() is used at:
13
- // https://github.com/vikejs/vike/blob/0b1b109f64aafbed23a1c2ac2630e6146a270ec0/packages/vike.dev/components/CommunityNote.tsx#L4
14
- export { usePageContext } from './renderer/usePageContext'
15
-
16
7
  export * from './components/Note'
17
8
  export * from './icons/index'
18
9
  export { assert } from './utils/assert'
19
10
  export { parseMarkdownMini } from './parseMarkdownMini'
20
11
  export type { Config } from './types/Config'
21
12
  export type { HeadingDefinition, HeadingDetachedDefinition } from './types/Heading'
13
+ // The only place usePageContext() is used at:
14
+ // https://github.com/vikejs/vike/blob/0b1b109f64aafbed23a1c2ac2630e6146a270ec0/packages/vike.dev/components/CommunityNote.tsx#L4
15
+ export { usePageContext } from './renderer/usePageContext'
16
+
17
+ // The following are used internally by DocPress — users (should) never use these exports
18
+ /************/
19
+ /* INTERNAL */
20
+ /************/
21
+ // We provide our own `useMDXComponents()` to enable MDX component injection by setting `providerImportSource` to '@brillout/docpress'.
22
+ // https://mdxjs.com/guides/injecting-components/
23
+ export { useMDXComponents } from './code-blocks/hooks/useMDXComponents'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brillout/docpress",
3
- "version": "0.15.10",
3
+ "version": "0.15.11-commit-e615832",
4
4
  "type": "module",
5
5
  "dependencies": {
6
6
  "@brillout/picocolors": "^1.0.10",
@@ -11,9 +11,11 @@
11
11
  "@mdx-js/rollup": "3.0.1",
12
12
  "@shikijs/transformers": "1.2.0",
13
13
  "@vitejs/plugin-react-swc": "^3.10.2",
14
+ "detype": "^1.1.3",
14
15
  "rehype-pretty-code": "0.13.0",
15
16
  "remark-gfm": "4.0.0",
16
17
  "shiki": "1.2.0",
18
+ "unist-util-visit": "^5.0.0",
17
19
  "vite": "^6.3.5"
18
20
  },
19
21
  "peerDependencies": {
@@ -58,9 +60,12 @@
58
60
  },
59
61
  "devDependencies": {
60
62
  "@brillout/release-me": "^0.4.8",
63
+ "@types/hast": "^3.0.4",
64
+ "@types/mdast": "^4.0.4",
61
65
  "@types/node": "^22.5.5",
62
66
  "@types/react": "^18.3.8",
63
- "@types/react-dom": "^18.3.0"
67
+ "@types/react-dom": "^18.3.0",
68
+ "mdast-util-mdx-jsx": "^3.2.0"
64
69
  },
65
70
  "repository": "https://github.com/brillout/docpress",
66
71
  "license": "MIT",
@@ -13,7 +13,7 @@ import type {
13
13
  HeadingDetachedResolved,
14
14
  StringArray,
15
15
  } from './types/Heading'
16
- import { assert } from './utils/assert'
16
+ import { assert, assertUsage } from './utils/assert'
17
17
  import { jsxToTextContent } from './utils/jsxToTextContent'
18
18
  import pc from '@brillout/picocolors'
19
19
  import { parseMarkdownMini } from './parseMarkdownMini'
@@ -149,6 +149,16 @@ function getActiveHeading(
149
149
  headingsDetachedResolved: HeadingDetachedResolved[],
150
150
  urlPathname: string,
151
151
  ) {
152
+ const URLs =
153
+ '\n' +
154
+ [...headingsResolved, ...headingsDetachedResolved]
155
+ .filter(Boolean)
156
+ .map((h) => h.url)
157
+ .sort()
158
+ .map((url) => ` ${url}`)
159
+ .join('\n')
160
+ const errNotFound = `URL ${pc.bold(urlPathname)} not found in following URLs:${URLs}`
161
+ const errFoundTwice = `URL ${pc.bold(urlPathname)} found twice in following URLs:${URLs}`
152
162
  let activeHeading: HeadingResolved | HeadingDetachedResolved | null = null
153
163
  let activeCategoryName = 'Miscellaneous'
154
164
  let headingCategory: string | undefined
@@ -158,6 +168,7 @@ function getActiveHeading(
158
168
  headingCategory = heading.title
159
169
  }
160
170
  if (heading.url === urlPathname) {
171
+ assertUsage(!activeHeading, errFoundTwice)
161
172
  activeHeading = heading
162
173
  assert(headingCategory)
163
174
  activeCategoryName = headingCategory
@@ -167,21 +178,14 @@ function getActiveHeading(
167
178
  }
168
179
  const isDetachedPage = !activeHeading
169
180
  if (!activeHeading) {
170
- activeHeading = headingsDetachedResolved.find(({ url }) => urlPathname === url) ?? null
171
- }
172
- if (!activeHeading) {
173
- throw new Error(
174
- [
175
- `URL ${pc.bold(urlPathname)} not found in following URLs:`,
176
- [...headingsResolved, ...headingsDetachedResolved]
177
- .filter(Boolean)
178
- .map((h) => h.url)
179
- .sort()
180
- .map((url) => ` ${url}`)
181
- .join('\n'),
182
- ].join('\n'),
183
- )
181
+ const found = headingsDetachedResolved.filter(({ url }) => urlPathname === url)
182
+ if (found.length > 0) {
183
+ assertUsage(found.length === 1, errFoundTwice)
184
+ assertUsage(!activeHeading, errFoundTwice)
185
+ activeHeading = found[0]!
186
+ }
184
187
  }
188
+ assertUsage(activeHeading, errNotFound)
185
189
  if (activeHeading.category) activeCategoryName = activeHeading.category
186
190
  return { activeHeading, isDetachedPage, activeCategoryName }
187
191
  }
package/tsconfig.json CHANGED
@@ -2,7 +2,8 @@
2
2
  "compilerOptions": {
3
3
  "outDir": "./dist/",
4
4
  "jsx": "react",
5
- "module": "ES2020",
5
+ "module": "ES2022",
6
+ "target": "ES2022",
6
7
  "moduleResolution": "Bundler",
7
8
  "lib": ["DOM", "DOM.Iterable", "ESNext"],
8
9
  "types": ["vite/client"],
package/vite.config.ts CHANGED
@@ -7,17 +7,27 @@ import { parsePageSections } from './parsePageSections.js'
7
7
  import rehypePrettyCode from 'rehype-pretty-code'
8
8
  import remarkGfm from 'remark-gfm'
9
9
  import { transformerNotationDiff } from '@shikijs/transformers'
10
+ import { rehypeMetaToProps } from './code-blocks/rehypeMetaToProps.js'
11
+ import { remarkDetype } from './code-blocks/remarkDetype.js'
12
+ import { shikiTransformerAutoLinks } from './code-blocks/shikiTransformerAutoLinks.js'
10
13
 
11
14
  const root = process.cwd()
12
- const prettyCode = [rehypePrettyCode, { theme: 'github-light', transformers: [transformerNotationDiff()] }]
13
- const rehypePlugins: any = [prettyCode]
14
- const remarkPlugins = [remarkGfm]
15
+ const prettyCode = [
16
+ rehypePrettyCode,
17
+ {
18
+ theme: 'github-light',
19
+ keepBackground: false,
20
+ transformers: [transformerNotationDiff(), shikiTransformerAutoLinks()],
21
+ },
22
+ ]
23
+ const rehypePlugins: any = [prettyCode, [rehypeMetaToProps]]
24
+ const remarkPlugins = [remarkGfm, remarkDetype]
15
25
 
16
26
  const config: UserConfig = {
17
27
  root,
18
28
  plugins: [
19
29
  parsePageSections(),
20
- mdx({ rehypePlugins, remarkPlugins }) as PluginOption,
30
+ mdx({ rehypePlugins, remarkPlugins, providerImportSource: '@brillout/docpress' }) as PluginOption,
21
31
  // @vitejs/plugin-react-swc needs to be added *after* the mdx plugins
22
32
  react(),
23
33
  ],