@nutin/cli 2.1.0 → 3.0.0

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 (241) hide show
  1. package/CHANGELOG.md +322 -0
  2. package/README.md +14 -6
  3. package/bin/add-feature.mjs +1 -1
  4. package/bin/cli.mjs +1 -1
  5. package/bin/create-app.mjs +1 -1
  6. package/bin/update-app.mjs +1 -1
  7. package/lib/index.mjs +6 -2
  8. package/lib/src/add-feature/base-template-patcher.mjs +139 -0
  9. package/lib/src/add-feature/feature-adder.mjs +20 -9
  10. package/lib/src/add-feature/feature-context-builder.mjs +1 -0
  11. package/lib/src/add-feature/nutin-config-updater.mjs +34 -8
  12. package/lib/src/add-feature/package-json-updater.mjs +7 -9
  13. package/lib/src/common/feature-registry.mjs +4 -0
  14. package/lib/src/common/file-generator.mjs +25 -3
  15. package/lib/src/common/git.mjs +31 -0
  16. package/lib/src/common/meta-bootstrap-prompt.mjs +28 -2
  17. package/lib/src/common/package-json-helper.mjs +14 -4
  18. package/lib/src/common/project-meta.mjs +2 -2
  19. package/lib/src/common/template-compiler.mjs +2 -0
  20. package/lib/src/common/transpile-ts.mjs +94 -0
  21. package/lib/src/create-app/context-builder.mjs +1 -0
  22. package/lib/src/create-app/git-manager.mjs +12 -2
  23. package/lib/src/create-app/json-manager.mjs +21 -18
  24. package/lib/src/create-app/new-app-prompt.mjs +4 -2
  25. package/lib/src/create-app/success-message.mjs +1 -1
  26. package/lib/src/update-app/conflict-report-writer.mjs +33 -21
  27. package/lib/src/update-app/json-drift.mjs +54 -0
  28. package/lib/src/update-app/project-updater.mjs +123 -14
  29. package/lib/src/update-app/template-differ.mjs +5 -5
  30. package/lib/src/update-app/three-way-merge.mjs +42 -0
  31. package/lib/src/update-app/update-context-builder.mjs +1 -0
  32. package/lib/src/update-app/update-summary-printer.mjs +10 -2
  33. package/package.json +12 -11
  34. package/templates/base/.gitignore.hbs +1 -0
  35. package/templates/base/AGENTS.md.hbs +45 -43
  36. package/templates/base/GETTING_STARTED.md.hbs +32 -23
  37. package/templates/base/README.md.hbs +7 -4
  38. package/templates/base/nutin.config.js.hbs +1 -1
  39. package/templates/base/src/app/components/.gitkeep +0 -0
  40. package/templates/base/src/app/globals.d.ts.hbs +6 -3
  41. package/templates/base/src/app/guards.ts.hbs +1 -3
  42. package/templates/base/src/app/main.ts.hbs +2 -9
  43. package/templates/base/src/app/routes.ts.hbs +9 -3
  44. package/templates/base/src/app/services/.gitkeep +0 -0
  45. package/templates/base/src/app/views/home/home.view.html.hbs +1 -1
  46. package/templates/base/src/app/views/home/home.view.ts.hbs +2 -2
  47. package/templates/base/src/app/views/not-found/not-found.view.ts.hbs +2 -2
  48. package/templates/base/src/core/base-classes/base-component/base-component.test.js.hbs +461 -1
  49. package/templates/base/src/core/base-classes/base-component/base-component.ts.hbs +39 -48
  50. package/templates/base/src/core/base-classes/base-component/helpers/catalog.helper.test.js.hbs +36 -1
  51. package/templates/base/src/core/base-classes/base-component/helpers/catalog.helper.ts.hbs +47 -77
  52. package/templates/base/src/core/base-classes/base-component/helpers/children.helper.test.js.hbs +307 -21
  53. package/templates/base/src/core/base-classes/base-component/helpers/children.helper.ts.hbs +144 -26
  54. package/templates/base/src/core/base-classes/base-component/helpers/dom.helper.test.js.hbs +105 -4
  55. package/templates/base/src/core/base-classes/base-component/helpers/dom.helper.ts.hbs +136 -62
  56. package/templates/base/src/core/base-classes/base-component/helpers/event.helper.test.js.hbs +85 -4
  57. package/templates/base/src/core/base-classes/base-component/helpers/event.helper.ts.hbs +101 -51
  58. package/templates/base/src/core/base-classes/base-component/helpers/html.helper.test.js.hbs +404 -0
  59. package/templates/base/src/core/base-classes/base-component/helpers/html.helper.ts.hbs +242 -0
  60. package/templates/base/src/core/base-classes/base-component/helpers/i18n.helper.test.js.hbs +70 -1
  61. package/templates/base/src/core/base-classes/base-component/helpers/i18n.helper.ts.hbs +54 -15
  62. package/templates/base/src/core/base-classes/base-component/helpers/pipe.helper.test.js.hbs +12 -1
  63. package/templates/base/src/core/base-classes/base-component/helpers/pipe.helper.ts.hbs +39 -36
  64. package/templates/base/src/core/base-classes/base-component/helpers/preservation.helper.test.js.hbs +179 -0
  65. package/templates/base/src/core/base-classes/base-component/helpers/preservation.helper.ts.hbs +93 -0
  66. package/templates/base/src/core/base-classes/base-component/helpers/security.helper.test.js.hbs +135 -17
  67. package/templates/base/src/core/base-classes/base-component/helpers/security.helper.ts.hbs +199 -81
  68. package/templates/base/src/core/base-classes/base-component/helpers/token.helper.test.js.hbs +40 -23
  69. package/templates/base/src/core/base-classes/base-component/helpers/token.helper.ts.hbs +67 -65
  70. package/templates/base/src/core/base-classes/component/component.test.js.hbs +17 -17
  71. package/templates/base/src/core/base-classes/component/component.ts.hbs +8 -41
  72. package/templates/base/src/core/base-classes/component/helpers/config.helper.test.js.hbs +1 -1
  73. package/templates/base/src/core/base-classes/component/helpers/config.helper.ts.hbs +37 -39
  74. package/templates/base/src/core/base-classes/component/helpers/data-binding.helper.test.js.hbs +14 -1
  75. package/templates/base/src/core/base-classes/component/helpers/data-binding.helper.ts.hbs +33 -31
  76. package/templates/base/src/core/base-classes/service/service.test.js.hbs +32 -0
  77. package/templates/base/src/core/base-classes/service/service.ts.hbs +21 -13
  78. package/templates/base/src/core/base-classes/view/view.test.js.hbs +4 -0
  79. package/templates/base/src/core/base-classes/view/view.ts.hbs +11 -8
  80. package/templates/base/src/core/index.ts.hbs +4 -2
  81. package/templates/base/src/core/internals.d.ts.hbs +199 -20
  82. package/templates/base/src/core/services/event-bus/event-bus.test.js.hbs +18 -0
  83. package/templates/base/src/core/services/event-bus/event-bus.ts.hbs +26 -25
  84. package/templates/base/src/core/services/event-bus/lifecycle.facade.ts.hbs +3 -3
  85. package/templates/base/src/core/services/event-bus/navigation.facade.ts.hbs +3 -3
  86. package/templates/base/src/core/services/globals/globals.ts.hbs +7 -32
  87. package/templates/base/src/core/services/http-client/helpers/http-builder.helper.test.js.hbs +133 -50
  88. package/templates/base/src/core/services/http-client/helpers/http-builder.helper.ts.hbs +147 -39
  89. package/templates/base/src/core/services/http-client/helpers/http-manager.helper.test.js.hbs +75 -19
  90. package/templates/base/src/core/services/http-client/helpers/http-manager.helper.ts.hbs +73 -47
  91. package/templates/base/src/core/services/http-client/http-client.test.js.hbs +339 -232
  92. package/templates/base/src/core/services/http-client/http-client.ts.hbs +37 -25
  93. package/templates/base/src/core/services/i18n/i18n.test.js.hbs +57 -16
  94. package/templates/base/src/core/services/i18n/i18n.ts.hbs +42 -42
  95. package/templates/base/src/core/services/i18n/languages.ts.hbs +0 -2
  96. package/templates/base/src/core/services/pipe-registry/pipe-registry.ts.hbs +4 -6
  97. package/templates/base/src/core/services/pipe-registry/pipes.test.js.hbs +10 -0
  98. package/templates/base/src/core/services/pipe-registry/pipes.ts.hbs +4 -2
  99. package/templates/base/src/core/services/router/helpers/navigation-manager.helper.test.js.hbs +217 -4
  100. package/templates/base/src/core/services/router/helpers/navigation-manager.helper.ts.hbs +210 -65
  101. package/templates/base/src/core/services/router/helpers/route-guard-manager.helper.test.js.hbs +26 -27
  102. package/templates/base/src/core/services/router/helpers/route-guard-manager.helper.ts.hbs +38 -60
  103. package/templates/base/src/core/services/router/helpers/view-render-manager.helper.test.js.hbs +4 -4
  104. package/templates/base/src/core/services/router/helpers/view-render-manager.helper.ts.hbs +30 -30
  105. package/templates/base/src/core/services/router/router.test.js.hbs +321 -0
  106. package/templates/base/src/core/services/router/router.ts.hbs +122 -54
  107. package/templates/base/src/index.html.hbs +1 -0
  108. package/templates/base/src/styles/_styles.scss.hbs +1 -113
  109. package/templates/base/src/styles/main.scss.hbs +1 -1
  110. package/templates/base/tools/builder/builder.config.js.hbs +6 -0
  111. package/templates/base/tools/builder/builder.js.hbs +26 -8
  112. package/templates/base/tools/builder/core/app/compile-ts.js.hbs +81 -6
  113. package/templates/base/tools/builder/core/app/copy-static.js.hbs +12 -1
  114. package/templates/base/tools/builder/core/app/handler-refs.js.hbs +114 -0
  115. package/templates/base/tools/builder/core/app/handler-refs.test.js.hbs +70 -0
  116. package/templates/base/tools/builder/core/app/paths.js.hbs +17 -0
  117. package/templates/base/tools/builder/core/app/route-keys.js.hbs +47 -0
  118. package/templates/base/tools/builder/core/app/route-keys.test.js.hbs +97 -0
  119. package/templates/base/tools/builder/core/app/ts-errors.js.hbs +58 -0
  120. package/templates/base/tools/builder/core/app/ts-errors.test.js.hbs +84 -0
  121. package/templates/base/tools/builder/core/app/validate-routes.js.hbs +21 -55
  122. package/templates/base/tools/builder/core/finalize-build.js.hbs +6 -0
  123. package/templates/base/tools/builder/core/html-index/add-tags.js.hbs +1 -4
  124. package/templates/base/tools/builder/core/html-index/add-tags.test.js.hbs +3 -3
  125. package/templates/base/tools/builder/core/html-index/validate-html.js.hbs +10 -2
  126. package/templates/base/tools/builder/core/html-templates/merge-templates.js.hbs +111 -23
  127. package/templates/base/tools/builder/core/html-templates/minify-templates.js.hbs +8 -0
  128. package/templates/base/tools/builder/core/html-templates/process-html-templates.js.hbs +5 -3
  129. package/templates/base/tools/builder/core/html-templates/template-literal.js.hbs +193 -0
  130. package/templates/base/tools/builder/core/html-templates/template-literal.test.js.hbs +69 -0
  131. package/templates/base/tools/builder/core/i18n/build-i18n.js.hbs +14 -6
  132. package/templates/base/tools/builder/core/prod-bundle/bundle-options.js.hbs +82 -0
  133. package/templates/base/tools/builder/core/prod-bundle/bundle-options.test.js.hbs +65 -0
  134. package/templates/base/tools/builder/core/prod-bundle/compress-files.js.hbs +6 -6
  135. package/templates/base/tools/builder/core/prod-bundle/esbuild.js.hbs +25 -8
  136. package/templates/base/tools/builder/core/prod-bundle/hash-files.js.hbs +4 -4
  137. package/templates/base/tools/builder/core/seo/generate-robots-txt.js.hbs +31 -13
  138. package/templates/base/tools/builder/core/seo/generate-robots-txt.test.js.hbs +48 -0
  139. package/templates/base/tools/builder/core/seo/generate-seo-files.js.hbs +3 -3
  140. package/templates/base/tools/builder/core/seo/generate-seo-html.js.hbs +61 -15
  141. package/templates/base/tools/builder/core/seo/generate-sitemap-xml.js.hbs +22 -7
  142. package/templates/base/tools/builder/core/seo/generate-sitemap-xml.test.js.hbs +30 -0
  143. package/templates/base/tools/builder/core/seo/html-substitutions.js.hbs +48 -18
  144. package/templates/base/tools/builder/core/seo/html-substitutions.test.js.hbs +45 -0
  145. package/templates/base/tools/builder/core/seo/route-output.js.hbs +26 -8
  146. package/templates/base/tools/builder/core/seo/route-output.test.js.hbs +31 -0
  147. package/templates/base/tools/builder/core/seo/seo-routes.js.hbs +29 -0
  148. package/templates/base/tools/builder/core/seo/seo-routes.test.js.hbs +21 -0
  149. package/templates/base/tools/builder/core/seo/ssr/ssr-bundle.js.hbs +6 -7
  150. package/templates/base/tools/builder/core/seo/ssr/ssr-polyfills.js.hbs +34 -5
  151. package/templates/base/tools/builder/core/seo/ssr/ssr-polyfills.test.js.hbs +48 -0
  152. package/templates/base/tools/builder/core/seo/ssr/ssr-render.js.hbs +26 -9
  153. package/templates/base/tools/builder/core/styles/sass.js.hbs +7 -5
  154. package/templates/base/tools/builder/core/styles/tailwind.js.hbs +19 -59
  155. package/templates/base/tools/dev/dev-serve.js.hbs +71 -7
  156. package/templates/base/tools/dev/serve.js.hbs +152 -22
  157. package/templates/base/tools/dev/watcher.js.hbs +75 -28
  158. package/templates/base/tools/generator/generator.js.hbs +23 -18
  159. package/templates/base/tools/generator/handle-file.js.hbs +4 -24
  160. package/templates/base/tools/generator/handle-file.test.js.hbs +4 -23
  161. package/templates/base/tools/generator/templates/component/component.template.js.hbs +3 -3
  162. package/templates/base/tools/generator/templates/component/component.template.test.js.hbs +3 -3
  163. package/templates/base/tools/generator/templates/service/service.template.js.hbs +1 -1
  164. package/templates/base/tools/generator/templates/service/service.template.test.js.hbs +1 -1
  165. package/templates/base/tools/generator/templates/view/view.template.js.hbs +2 -2
  166. package/templates/base/tools/generator/templates/view/view.template.test.js.hbs +2 -2
  167. package/templates/base/tools/testin-nutin/core/coverage/write-uncovered-report.js.hbs +1 -1
  168. package/templates/base/tools/testin-nutin/core/globals/assertion-lib.js.hbs +2 -2
  169. package/templates/base/tools/testin-nutin/core/{tests/assertions.test.js.hbs → globals/assertion-lib.test.js.hbs} +6 -0
  170. package/templates/base/tools/testin-nutin/core/globals/register-test-globals.js.hbs +6 -7
  171. package/templates/base/tools/testin-nutin/core/{tests/globals-each-all.test.js.hbs → globals/register-test-globals.test.js.hbs} +12 -0
  172. package/templates/base/tools/testin-nutin/core/printer.js.hbs +10 -4
  173. package/templates/base/tools/testin-nutin/core/queue/test-queue.js.hbs +8 -8
  174. package/templates/base/tools/testin-nutin/mocks/mock-event-bus.js.hbs +27 -25
  175. package/templates/base/tools/testin-nutin/mocks/mock-event-bus.test.js.hbs +30 -22
  176. package/templates/base/tools/testin-nutin/mocks/mock-http-client.js.hbs +7 -10
  177. package/templates/base/tools/testin-nutin/mocks/mock-http-client.test.js.hbs +12 -0
  178. package/templates/base/tools/testin-nutin/mocks/mock-i18n.js.hbs +47 -22
  179. package/templates/base/tools/testin-nutin/mocks/mock-i18n.test.js.hbs +47 -26
  180. package/templates/base/tools/testin-nutin/mocks/mock-router.js.hbs +19 -11
  181. package/templates/base/tools/testin-nutin/mocks/mock-router.test.js.hbs +24 -7
  182. package/templates/base/tools/testin-nutin/runner.js.hbs +12 -3
  183. package/templates/base/tools/testin-nutin/watch-tests.js.hbs +45 -20
  184. package/templates/base/tools/utils/build-lock.js.hbs +97 -0
  185. package/templates/base/tools/utils/ensure-deps.js.hbs +72 -0
  186. package/templates/base/tools/utils/ensure-deps.test.js.hbs +17 -0
  187. package/templates/base/tools/utils/format.js.hbs +1 -1
  188. package/templates/base/tools/utils/index.js.hbs +3 -1
  189. package/templates/base/tools/utils/print.js.hbs +2 -2
  190. package/templates/base/tools/utils/resolve-port.js.hbs +30 -0
  191. package/templates/base/tools/utils/resolve-port.test.js.hbs +47 -0
  192. package/templates/base/tools/utils/run-command.js.hbs +30 -1
  193. package/templates/base/tools/utils/run-command.test.js.hbs +12 -1
  194. package/templates/base/tools/utils/run-script.js.hbs +2 -2
  195. package/templates/features/docker/tools/docker/Dockerfile.template.hbs +5 -2
  196. package/templates/features/docker/tools/docker/nginx.conf.template +19 -2
  197. package/templates/features/docker/tools/docker/scripts/docker-ports.js.hbs +1 -1
  198. package/templates/features/docker/tools/docker/scripts/docker-ports.test.js.hbs +2 -2
  199. package/templates/features/docker/tools/docker/scripts/locale-redirects.js.hbs +28 -0
  200. package/templates/features/docker/tools/docker/scripts/locale-redirects.test.js.hbs +34 -0
  201. package/templates/features/docker/tools/docker/scripts/markdown-sources-copy.js.hbs +25 -0
  202. package/templates/features/docker/tools/docker/scripts/markdown-sources-copy.test.js.hbs +34 -0
  203. package/templates/features/docker/tools/docker/scripts/render-docker-template.js.hbs +3 -1
  204. package/templates/features/docker/tools/docker/scripts/render-docker-template.test.js.hbs +12 -0
  205. package/templates/features/docker/tools/docker/scripts/validate-docker.js.hbs +29 -0
  206. package/templates/features/markdown/markdown-content/getting-started.md.hbs +28 -0
  207. package/templates/features/markdown/markdown-content/writing-pages.md.hbs +19 -0
  208. package/templates/features/markdown/src/app/markdown/components/markdown-content/markdown-content.component.scss.hbs +75 -0
  209. package/templates/features/markdown/src/app/markdown/components/markdown-content/markdown-content.component.test.js.hbs +40 -0
  210. package/templates/features/markdown/src/app/markdown/components/markdown-content/markdown-content.component.ts.hbs +65 -0
  211. package/templates/features/markdown/src/app/markdown/components/markdown-landing/markdown-landing.component.test.js.hbs +67 -0
  212. package/templates/features/markdown/src/app/markdown/components/markdown-landing/markdown-landing.component.ts.hbs +87 -0
  213. package/templates/features/markdown/src/app/markdown/components/markdown-nav/markdown-nav.component.scss.hbs +42 -0
  214. package/templates/features/markdown/src/app/markdown/components/markdown-nav/markdown-nav.component.test.js.hbs +59 -0
  215. package/templates/features/markdown/src/app/markdown/components/markdown-nav/markdown-nav.component.ts.hbs +72 -0
  216. package/templates/features/markdown/src/app/markdown/locales/en.json.hbs +6 -0
  217. package/templates/features/markdown/src/app/markdown/markdown-guards.test.js.hbs +63 -0
  218. package/templates/features/markdown/src/app/markdown/markdown-guards.ts.hbs +31 -0
  219. package/templates/features/markdown/src/app/markdown/markdown-i18n.test.js.hbs +78 -0
  220. package/templates/features/markdown/src/app/markdown/markdown-i18n.ts.hbs +31 -0
  221. package/templates/features/markdown/src/app/markdown/markdown-manifest.test.js.hbs +98 -0
  222. package/templates/features/markdown/src/app/markdown/markdown-manifest.ts.hbs +157 -0
  223. package/templates/features/markdown/src/app/markdown/markdown-manifests.service.test.js.hbs +87 -0
  224. package/templates/features/markdown/src/app/markdown/markdown-manifests.service.ts.hbs +50 -0
  225. package/templates/features/markdown/src/app/markdown/markdown-routes.test.js.hbs +74 -0
  226. package/templates/features/markdown/src/app/markdown/markdown-routes.ts.hbs +26 -0
  227. package/templates/features/markdown/src/app/markdown/markdown-sources.test.js.hbs +22 -0
  228. package/templates/features/markdown/src/app/markdown/markdown-sources.ts.hbs +42 -0
  229. package/templates/features/markdown/src/app/markdown/view/markdown.view.html.hbs +4 -0
  230. package/templates/features/markdown/src/app/markdown/view/markdown.view.scss.hbs +19 -0
  231. package/templates/features/markdown/src/app/markdown/view/markdown.view.test.js.hbs +125 -0
  232. package/templates/features/markdown/src/app/markdown/view/markdown.view.ts.hbs +146 -0
  233. package/templates/features/markdown/tools/builder/core/markdown/generate-markdown-pages.js.hbs +71 -0
  234. package/templates/features/markdown/tools/builder/core/markdown/markdown-compiler.js.hbs +582 -0
  235. package/templates/features/markdown/tools/builder/core/markdown/markdown-compiler.test.js.hbs +390 -0
  236. package/templates/features/markdown/tools/builder/core/markdown/markdown-seo-routes.js.hbs +155 -0
  237. package/templates/features/markdown/tools/builder/core/markdown/markdown-seo-routes.test.js.hbs +112 -0
  238. package/templates/base/src/app/components/index.ts.hbs +0 -1
  239. package/templates/base/src/app/services/index.ts.hbs +0 -1
  240. package/templates/base/src/app/views/index.ts.hbs +0 -3
  241. /package/templates/base/tools/testin-nutin/core/{tests → globals}/clock.test.js.hbs +0 -0
@@ -0,0 +1,146 @@
1
+ import { Component, Navigation, NavigationManager, View, html } from '../../../core/index.js';
2
+ import { IMarkdownPage, IMarkdownSection, MarkdownManifest } from '../markdown-manifest.js';
3
+ import { MarkdownManifestsService } from '../markdown-manifests.service.js';
4
+ import { IMarkdownContentConfig, MarkdownContentComponent } from '../components/markdown-content/markdown-content.component.js';
5
+ import { IMarkdownLandingConfig, MarkdownLandingComponent } from '../components/markdown-landing/markdown-landing.component.js';
6
+ import { IMarkdownNavConfig, MarkdownNavComponent } from '../components/markdown-nav/markdown-nav.component.js';
7
+ import { onMarkdownLanguageChange } from '../markdown-i18n.js';
8
+
9
+ const defaultTemplate = html`__TEMPLATE_PLACEHOLDER__`;
10
+
11
+ export type MarkdownComponentClass<C> = new (mountTarget: HTMLElement, config: C) => Component<HTMLElement, C>;
12
+
13
+ export interface IMarkdownViewOptions {
14
+ // The source's id: its "routePrefix" in nutin.config.js, defaulting to the folder name.
15
+ id: string;
16
+ // A custom layout needs data-component="markdown-nav" and data-component="markdown-content"
17
+ // (the content placeholder also receives the landing). Leave out "markdown-nav" for no nav.
18
+ template?: Nutin.Template;
19
+ viewName?: string;
20
+ // Replace a part of the page with your own component class, e.g. a subclass with its own
21
+ // template (see the components' optional `templateFn` constructor argument).
22
+ navComponent?: MarkdownComponentClass<IMarkdownNavConfig>;
23
+ contentComponent?: MarkdownComponentClass<IMarkdownContentConfig>;
24
+ landingComponent?: MarkdownComponentClass<IMarkdownLandingConfig>;
25
+ // Called after a page's content is rendered, e.g. to highlight its code blocks.
26
+ onContentRendered?: (element: HTMLElement, page: IMarkdownPage) => void;
27
+ }
28
+
29
+ // Renders one page of a "markdownSources" folder. markdownRoutes() registers one per folder;
30
+ // with "sectionInPath: true" the route carries ':section?' before ':slug?' (e.g.
31
+ // '/docs/api/navigate'), the nav only lists that section and every URL includes it.
32
+ // Subclass it to change more: its getters and helpers are protected.
33
+ export class MarkdownView extends View {
34
+ protected readonly manifest: MarkdownManifest;
35
+ protected readonly options: IMarkdownViewOptions;
36
+ private stopLanguageSync: () => void = () => {};
37
+
38
+ constructor(options: IMarkdownViewOptions) {
39
+ const { id, template = defaultTemplate, viewName = id } = options;
40
+ super({ template, viewName });
41
+ this.options = options;
42
+ this.manifest = MarkdownManifestsService.get(id);
43
+ }
44
+
45
+ protected get section(): IMarkdownSection | undefined {
46
+ if (!this.manifest.sectionInPath) return undefined;
47
+ return this.manifest.getSection(this.getRouteParam('section') ?? '') ?? this.manifest.sections[0];
48
+ }
49
+
50
+ // A bare route (no slug) of a folder with "landing": its index is shown instead of a page.
51
+ protected get isLanding(): boolean {
52
+ return !!this.manifest.landing && !this.getRouteParam('slug');
53
+ }
54
+
55
+ // MarkdownGuards already loaded the manifest and checked this slug resolves to a real
56
+ // page (or that the folder has no pages yet) before the view is constructed.
57
+ protected get slug(): string {
58
+ if (this.isLanding) return '';
59
+ const fallback = this.manifest.sectionInPath ? this.manifest.firstSlugOf(this.section) : this.manifest.firstSlug;
60
+ return this.getRouteParam('slug') || fallback || '';
61
+ }
62
+
63
+ protected get page(): IMarkdownPage | undefined {
64
+ return this.slug ? this.manifest.getPage(this.slug) : undefined;
65
+ }
66
+
67
+ protected get navSections(): IMarkdownSection[] {
68
+ if (!this.manifest.sectionInPath) return this.manifest.sections;
69
+ // The folder's own landing lists every section, so its nav does too.
70
+ if (this.isLanding && !this.getRouteParam('section')) return this.manifest.sections;
71
+ return this.section ? [this.section] : [];
72
+ }
73
+
74
+ protected pageHref(slug: string): string {
75
+ const section = this.manifest.sectionInPath ? this.manifest.getPage(slug)?.section : undefined;
76
+ return section ? `/${this.manifest.id}/${section}/${slug}` : `/${this.manifest.id}/${slug}`;
77
+ }
78
+
79
+ // The landing shown: the folder's (every section), or one section's with "sectionInPath".
80
+ protected landingConfig(): IMarkdownLandingConfig {
81
+ const localized = (path: string) => NavigationManager.addLocalePrefix(path);
82
+ const sectionId = this.getRouteParam('section');
83
+ const section = sectionId ? this.manifest.getSection(sectionId) : undefined;
84
+ const pageHref = (slug: string) => localized(this.pageHref(slug));
85
+
86
+ if (section) {
87
+ return { title: section.title, description: section.description, sections: [section], manifest: this.manifest, pageHref };
88
+ }
89
+ const landing = this.manifest.landing ?? { title: this.viewName, description: '' };
90
+ return {
91
+ title: landing.title,
92
+ description: landing.description,
93
+ sections: this.manifest.sections,
94
+ manifest: this.manifest,
95
+ pageHref,
96
+ // With sections in the URL, each one has its own landing: link to it instead of listing its pages.
97
+ ...(this.manifest.sectionInPath ? { sectionHref: (id: string) => localized(`/${this.manifest.id}/${id}`) } : {}),
98
+ };
99
+ }
100
+
101
+ // document.title after each navigation: the page's (or landing's) title, as in its SEO page.
102
+ public override documentTitle(): string | undefined {
103
+ if (this.isLanding) return this.landingConfig().title;
104
+ return this.page?.title;
105
+ }
106
+
107
+ // The bare route (e.g. /content) renders the first page through the fallback above;
108
+ // point the URL at that page so reloads and bookmarks match what is shown.
109
+ public onEnter(): void {
110
+ if (!this.isLanding && !this.getRouteParam('slug') && this.slug) {
111
+ NavigationManager.replaceState(this.pageHref(this.slug));
112
+ }
113
+ // With i18n, a language change reloads the route: MarkdownGuards load the new
114
+ // language's manifest, and the same page renders in that language.
115
+ this.stopLanguageSync = onMarkdownLanguageChange(() => Navigation.reload());
116
+ }
117
+
118
+ public onExit(): void {
119
+ this.stopLanguageSync();
120
+ }
121
+
122
+ public registerChildren(): Nutin.ComponentConfig[] {
123
+ const Nav = this.options.navComponent ?? MarkdownNavComponent;
124
+ const Content = this.options.contentComponent ?? MarkdownContentComponent;
125
+ const Landing = this.options.landingComponent ?? MarkdownLandingComponent;
126
+
127
+ return [
128
+ {
129
+ selector: 'markdown-nav',
130
+ factory: (el) => new Nav(el, {
131
+ sections: this.navSections,
132
+ manifest: this.manifest,
133
+ currentSlug: this.slug,
134
+ // The links' hrefs carry the locale prefix with i18n (replaceState adds it itself).
135
+ pageHref: (slug) => NavigationManager.addLocalePrefix(this.pageHref(slug)),
136
+ }),
137
+ },
138
+ {
139
+ selector: 'markdown-content',
140
+ factory: (el) => this.isLanding
141
+ ? new Landing(el, this.landingConfig())
142
+ : new Content(el, { page: this.page, loadFailed: this.manifest.loadFailed, onRendered: this.options.onContentRendered }),
143
+ },
144
+ ];
145
+ }
146
+ }
@@ -0,0 +1,71 @@
1
+ import * as fs from 'fs';
2
+ import path from 'path';
3
+ import { print, ensureDeps, errorExit } from '../../../utils/index.js';
4
+ import { PATHS } from '../app/paths.js';
5
+ import { builderConfig } from '../../builder.config.js';
6
+ import nutinConfig from '../../../../nutin.config.js';
7
+ import { LANGUAGES, DEFAULT_LANGUAGE } from '../../../utils/languages.js';
8
+ import { compileLanguages, normalizeSource, MarkdownCompileError } from './markdown-compiler.js';
9
+
10
+ // Compiles every `markdownSources.sourceFolders` entry of nutin.config.js into
11
+ // dist/generated/<routePrefix>.json — or, with i18n, <routePrefix>.<lang>.json per language —
12
+ // fetched at runtime by a MarkdownManifest.
13
+ // `--check` (the "markdown:check" script) compiles everything but writes nothing.
14
+
15
+ const isCheck = process.argv.includes('--check');
16
+
17
+ const sourceFolders = nutinConfig.markdownSources?.sourceFolders;
18
+ if (!Array.isArray(sourceFolders)) {
19
+ errorExit('nutin.config.js "markdownSources.sourceFolders" must be an array.', 'markdown');
20
+ }
21
+
22
+ await ensureDeps({
23
+ feature: 'Markdown (markdownSources)',
24
+ deps: [
25
+ { name: 'marked', version: '^18.0.14' },
26
+ { name: 'gray-matter', version: '^4.0.3' },
27
+ ],
28
+ isProd: builderConfig.isProd,
29
+ origin: 'markdown',
30
+ });
31
+
32
+ // Loaded only now: they may have just been installed.
33
+ const { Marked } = await import('marked');
34
+ const matter = (await import('gray-matter')).default;
35
+
36
+ const root = process.cwd();
37
+ const outputDir = path.join(PATHS.tempSource, 'generated');
38
+
39
+ try {
40
+ const sources = sourceFolders.map((entry) => normalizeSource(entry, root));
41
+
42
+ const seenIds = new Map();
43
+ for (const source of sources) {
44
+ if (seenIds.has(source.id)) {
45
+ throw new MarkdownCompileError(`"${seenIds.get(source.id)}" and "${source.folder}" both compile to "${source.id}.json" — give one a different "routePrefix"`);
46
+ }
47
+ seenIds.set(source.id, source.folder);
48
+ }
49
+
50
+ const languageOptions = { i18n: Boolean(nutinConfig.i18n), languages: LANGUAGES, defaultLanguage: DEFAULT_LANGUAGE };
51
+
52
+ for (const source of sources) {
53
+ const { manifests, warnings } = compileLanguages(source, { Marked, matter, root }, languageOptions);
54
+ warnings.forEach((warning) => print.warn(`markdown: ${warning}`));
55
+
56
+ for (const [lang, manifest] of Object.entries(manifests)) {
57
+ const fileName = lang ? `${source.id}.${lang}.json` : `${source.id}.json`;
58
+ const pageCount = Object.keys(manifest.pages).length;
59
+ if (isCheck) {
60
+ print.info(`markdown: "${source.folder}"${lang ? ` (${lang})` : ''} OK (${pageCount} pages)`);
61
+ continue;
62
+ }
63
+
64
+ fs.mkdirSync(outputDir, { recursive: true });
65
+ fs.writeFileSync(path.join(outputDir, fileName), JSON.stringify(manifest));
66
+ print.gray(`markdown: ${pageCount} pages from "${source.folder}" -> /generated/${fileName}`);
67
+ }
68
+ }
69
+ } catch (err) {
70
+ errorExit(err instanceof MarkdownCompileError ? err.message : err, 'markdown');
71
+ }