@brillout/docpress 0.7.0 → 0.7.2

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.
@@ -13,6 +13,7 @@ import { NavigationData, NavItem } from '../navigation/Navigation'
13
13
  import type { LinkData } from '../components'
14
14
  import type { Exports, PageContextOriginal } from './resolvePageContext'
15
15
  import pc from '@brillout/picocolors'
16
+ import { parseTitle } from '../parseTitle'
16
17
 
17
18
  type PageSectionResolved = {
18
19
  url: string | null
@@ -130,7 +131,7 @@ function getTitles(
130
131
 
131
132
  const { title } = activeHeading
132
133
  let pageTitle = isLandingPage ? null : title
133
- let documentTitle = activeHeading.titleDocument || jsxToTextContent(title)
134
+ let documentTitle = activeHeading.titleDocument || jsxToTextContent(parseTitle(title))
134
135
 
135
136
  if (!isLandingPage) {
136
137
  documentTitle += ' | ' + config.projectInfo.projectName
@@ -22,6 +22,7 @@ export { resolveHeadingsData };
22
22
  import { assert, jsxToTextContent } from '../utils/server';
23
23
  import { getConfig } from './getConfig';
24
24
  import pc from '@brillout/picocolors';
25
+ import { parseTitle } from '../parseTitle';
25
26
  function resolveHeadingsData(pageContext) {
26
27
  var config = getConfig();
27
28
  {
@@ -111,7 +112,7 @@ function getTitles(activeHeading, pageContext, config) {
111
112
  var isLandingPage = url === '/';
112
113
  var title = activeHeading.title;
113
114
  var pageTitle = isLandingPage ? null : title;
114
- var documentTitle = activeHeading.titleDocument || jsxToTextContent(title);
115
+ var documentTitle = activeHeading.titleDocument || jsxToTextContent(parseTitle(title));
115
116
  if (!isLandingPage) {
116
117
  documentTitle += ' | ' + config.projectInfo.projectName;
117
118
  }
@@ -1,6 +1,24 @@
1
1
  export { assert };
2
2
  export { assertUsage };
3
3
  export { assertWarning };
4
+ var devModeKey = '__docpress_dev_mode';
5
+ if (isBrowser()) {
6
+ ;
7
+ window.toggleDevMode = toggleDevMode;
8
+ console.log([
9
+ '[@brillout/docpress] DEV MODE',
10
+ isDevMode() ? 'enabled' : 'disabled',
11
+ !isLocalhost() && 'run window.toggleDevMode() to toggle DEV MODE',
12
+ ]
13
+ .filter(Boolean)
14
+ .join(' '));
15
+ if (isDevMode()) {
16
+ window.onerror = function (err) {
17
+ alert(err);
18
+ window.onerror = null;
19
+ };
20
+ }
21
+ }
4
22
  function assert(condition, debugInfo) {
5
23
  if (condition) {
6
24
  return;
@@ -17,8 +35,8 @@ function assert(condition, debugInfo) {
17
35
  errMsg += ' Debug info: ' + String(debugInfo);
18
36
  }
19
37
  var err = new Error(errMsg);
20
- if (isBrowserAndDev()) {
21
- alert(err.stack);
38
+ if (isBrowser() && isDevMode()) {
39
+ window.alert(err.stack);
22
40
  }
23
41
  throw err;
24
42
  }
@@ -27,14 +45,32 @@ function assertUsage(condition, msg) {
27
45
  return;
28
46
  }
29
47
  var err = new Error('[DocPress][Wrong Usage] ' + msg);
30
- if (isBrowserAndDev()) {
31
- alert(err.stack);
48
+ if (isBrowser() && isDevMode()) {
49
+ window.alert(err.stack);
32
50
  }
33
51
  throw err;
34
52
  }
35
- function isBrowserAndDev() {
53
+ function isBrowser() {
54
+ return typeof window !== 'undefined';
55
+ }
56
+ function isDevMode() {
57
+ return !!window.localStorage[devModeKey] || isLocalhost();
58
+ }
59
+ function isLocalhost() {
36
60
  var _a;
37
- return typeof window !== 'undefined' && ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.port) !== '';
61
+ return ((_a = window === null || window === void 0 ? void 0 : window.location) === null || _a === void 0 ? void 0 : _a.port) !== '';
62
+ }
63
+ function toggleDevMode() {
64
+ if (isLocalhost())
65
+ throw new Error('On localhost DEV MODE is always on.');
66
+ var isEnabled = function () { return window.localStorage[devModeKey]; };
67
+ if (!isEnabled()) {
68
+ window.localStorage[devModeKey] = 'true';
69
+ }
70
+ else {
71
+ delete window.localStorage[devModeKey];
72
+ }
73
+ console.log("DEV MODE ".concat(isEnabled() ? 'enabled' : 'disabled'));
38
74
  }
39
75
  function assertWarning(condition, msg) {
40
76
  if (condition) {
@@ -42,7 +78,7 @@ function assertWarning(condition, msg) {
42
78
  }
43
79
  msg = '[DocPress][Warning] ' + msg;
44
80
  console.warn(msg);
45
- if (isBrowserAndDev()) {
46
- alert(msg);
81
+ if (isBrowser() && isDevMode()) {
82
+ window.alert(msg);
47
83
  }
48
84
  }
@@ -1,2 +1,2 @@
1
1
  export { jsxToTextContent };
2
- declare function jsxToTextContent(node: JSX.Element | string): string;
2
+ declare function jsxToTextContent(node: JSX.Element): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brillout/docpress",
3
- "version": "0.7.0",
3
+ "version": "0.7.2",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "======== Build": "",
@@ -20,6 +20,7 @@ initOnLinkClick()
20
20
 
21
21
  let root: ReactDOM.Root
22
22
  function onRenderClient(pageContext: PageContextClient) {
23
+ // TODO: stop using any
23
24
  const pageContextResolved: PageContextResolved = (pageContext as any).pageContextResolved
24
25
  let page = getPageElement(pageContext, pageContextResolved)
25
26
  page = <OnRenderDoneHook>{page}</OnRenderDoneHook>
@@ -32,6 +33,16 @@ function onRenderClient(pageContext: PageContextClient) {
32
33
  }
33
34
  root.render(page)
34
35
  }
36
+
37
+ if (!pageContext.isHydration) {
38
+ applyHead(pageContext)
39
+ }
40
+ }
41
+
42
+ function applyHead(pageContext: PageContextClient) {
43
+ // TODO: stop using any
44
+ const pageContextResolved: PageContextResolved = (pageContext as any).pageContextResolved
45
+ document.title = pageContextResolved.documentTitle
35
46
  }
36
47
 
37
48
  function onRenderDone() {
@@ -3,11 +3,14 @@ export { onRenderHtml }
3
3
  import ReactDOMServer from 'react-dom/server'
4
4
  import { escapeInject, dangerouslySkipEscape } from 'vike/server'
5
5
  import { assert } from '../utils/server'
6
- import type { PageContextServer } from 'vike/types'
7
6
  import type { PageContextResolved } from '../config/resolvePageContext'
8
7
  import { getPageElement } from './getPageElement'
8
+ import type { OnRenderHtmlAsync } from 'vike/types'
9
9
 
10
- async function onRenderHtml(pageContext: PageContextServer) {
10
+ const onRenderHtml: OnRenderHtmlAsync = async (
11
+ pageContext,
12
+ ): // TODO: Why is Promise<Awaited<>> needed?
13
+ Promise<Awaited<ReturnType<OnRenderHtmlAsync>>> => {
11
14
  const pageContextResolved: PageContextResolved = (pageContext as any).pageContextResolved
12
15
 
13
16
  const page = getPageElement(pageContext, pageContextResolved)
package/utils/assert.ts CHANGED
@@ -2,6 +2,27 @@ export { assert }
2
2
  export { assertUsage }
3
3
  export { assertWarning }
4
4
 
5
+ const devModeKey = '__docpress_dev_mode'
6
+
7
+ if (isBrowser()) {
8
+ ;(window as any).toggleDevMode = toggleDevMode
9
+ console.log(
10
+ [
11
+ '[@brillout/docpress] DEV MODE',
12
+ isDevMode() ? 'enabled' : 'disabled',
13
+ !isLocalhost() && 'run window.toggleDevMode() to toggle DEV MODE',
14
+ ]
15
+ .filter(Boolean)
16
+ .join(' '),
17
+ )
18
+ if (isDevMode()) {
19
+ window.onerror = (err) => {
20
+ alert(err)
21
+ window.onerror = null
22
+ }
23
+ }
24
+ }
25
+
5
26
  function assert(condition: unknown, debugInfo?: unknown): asserts condition {
6
27
  if (condition) {
7
28
  return
@@ -18,8 +39,8 @@ function assert(condition: unknown, debugInfo?: unknown): asserts condition {
18
39
  errMsg += ' Debug info: ' + String(debugInfo)
19
40
  }
20
41
  const err = new Error(errMsg)
21
- if (isBrowserAndDev()) {
22
- alert(err.stack)
42
+ if (isBrowser() && isDevMode()) {
43
+ window.alert(err.stack)
23
44
  }
24
45
  throw err
25
46
  }
@@ -29,14 +50,30 @@ function assertUsage(condition: unknown, msg: string): asserts condition {
29
50
  return
30
51
  }
31
52
  const err = new Error('[DocPress][Wrong Usage] ' + msg)
32
- if (isBrowserAndDev()) {
33
- alert(err.stack)
53
+ if (isBrowser() && isDevMode()) {
54
+ window.alert(err.stack)
34
55
  }
35
56
  throw err
36
57
  }
37
58
 
38
- function isBrowserAndDev() {
39
- return typeof window !== 'undefined' && window?.location?.port !== ''
59
+ function isBrowser() {
60
+ return typeof window !== 'undefined'
61
+ }
62
+ function isDevMode() {
63
+ return !!window.localStorage[devModeKey] || isLocalhost()
64
+ }
65
+ function isLocalhost() {
66
+ return window?.location?.port !== ''
67
+ }
68
+ function toggleDevMode() {
69
+ if (isLocalhost()) throw new Error('On localhost DEV MODE is always on.')
70
+ const isEnabled = () => window.localStorage[devModeKey]
71
+ if (!isEnabled()) {
72
+ window.localStorage[devModeKey] = 'true'
73
+ } else {
74
+ delete window.localStorage[devModeKey]
75
+ }
76
+ console.log(`DEV MODE ${isEnabled() ? 'enabled' : 'disabled'}`)
40
77
  }
41
78
 
42
79
  function assertWarning(condition: unknown, msg: string): asserts condition {
@@ -45,7 +82,7 @@ function assertWarning(condition: unknown, msg: string): asserts condition {
45
82
  }
46
83
  msg = '[DocPress][Warning] ' + msg
47
84
  console.warn(msg)
48
- if (isBrowserAndDev()) {
49
- alert(msg)
85
+ if (isBrowser() && isDevMode()) {
86
+ window.alert(msg)
50
87
  }
51
88
  }
@@ -3,7 +3,7 @@ import { assert } from './assert'
3
3
  export { jsxToTextContent }
4
4
 
5
5
  // https://stackoverflow.com/questions/34204975/react-is-there-something-similar-to-node-textcontent/60564620#60564620
6
- function jsxToTextContent(node: JSX.Element | string): string {
6
+ function jsxToTextContent(node: JSX.Element): string {
7
7
  if (['string', 'number'].includes(typeof node)) return String(node)
8
8
  if (node instanceof Array) return node.map(jsxToTextContent).join('')
9
9
  if (typeof node === 'object' && node) return jsxToTextContent(node.props.children)