erudit 3.0.0-dev.17 → 3.0.0-dev.19

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 (250) hide show
  1. package/app/app.vue +193 -191
  2. package/app/assets/icons/graduation.svg +3 -0
  3. package/app/components/Loading.vue +23 -23
  4. package/app/components/SiteAside.vue +393 -393
  5. package/app/components/SiteMain.vue +32 -32
  6. package/app/components/aside/AsideListItem.vue +74 -74
  7. package/app/components/aside/AsideMajor.vue +56 -56
  8. package/app/components/aside/AsideMinor.vue +97 -73
  9. package/app/components/aside/major/PaneContentScroll.vue +23 -23
  10. package/app/components/aside/major/PaneSwitch.vue +54 -54
  11. package/app/components/aside/major/PaneSwitchButton.vue +63 -63
  12. package/app/components/aside/major/SiteInfo.vue +85 -85
  13. package/app/components/aside/major/panes/Language.vue +79 -79
  14. package/app/components/aside/major/panes/Pages.vue +31 -34
  15. package/app/components/aside/major/panes/Search.vue +11 -11
  16. package/app/components/aside/major/panes/nav/Nav.vue +92 -92
  17. package/app/components/aside/major/panes/nav/NavBook.vue +95 -95
  18. package/app/components/aside/major/panes/nav/NavBookLoading.vue +24 -24
  19. package/app/components/aside/major/panes/nav/NavGlobal.vue +16 -16
  20. package/app/components/aside/major/panes/nav/fnav/FNav.vue +105 -105
  21. package/app/components/aside/major/panes/nav/fnav/FNavBook.vue +32 -32
  22. package/app/components/aside/major/panes/nav/fnav/FNavFlags.vue +40 -40
  23. package/app/components/aside/major/panes/nav/fnav/FNavFolder.vue +60 -60
  24. package/app/components/aside/major/panes/nav/fnav/FNavItem.vue +34 -34
  25. package/app/components/aside/major/panes/nav/fnav/FNavSeparator.vue +80 -80
  26. package/app/components/aside/major/panes/nav/fnav/FNavTopic.vue +24 -24
  27. package/app/components/aside/major/panes/other/ItemContent.vue +29 -29
  28. package/app/components/aside/major/panes/other/ItemGenerator.vue +13 -13
  29. package/app/components/aside/major/panes/other/ItemTheme.vue +54 -54
  30. package/app/components/aside/major/panes/other/Other.vue +16 -16
  31. package/app/components/aside/minor/{Contribute.vue → AsideMinorContribute.vue} +175 -145
  32. package/app/components/aside/minor/AsideMinorNews.vue +11 -11
  33. package/app/components/aside/minor/AsideMinorPane.vue +15 -15
  34. package/app/components/aside/minor/AsideMinorTopLink.vue +67 -67
  35. package/app/components/aside/minor/content/AsideMinorContent.vue +89 -92
  36. package/app/components/aside/minor/contributor/AsideMinorContributor.vue +78 -0
  37. package/app/components/aside/minor/contributor/BookContribution.vue +64 -0
  38. package/app/components/aside/minor/topic/AsideMinorTopic.vue +29 -32
  39. package/app/components/aside/minor/topic/TopicContributors.vue +177 -177
  40. package/app/components/aside/minor/topic/TopicNav.vue +49 -49
  41. package/app/components/aside/minor/topic/TopicToc.vue +213 -214
  42. package/app/components/aside/minor/topic/TopicTocItem.vue +32 -32
  43. package/app/components/aside/utils/AsideOverlayPane.vue +40 -40
  44. package/app/components/bitran/BitranContent.vue +90 -91
  45. package/app/components/bitran/RenderWrapper.vue +10 -10
  46. package/app/components/contributor/ContributorAvatar.vue +45 -45
  47. package/app/components/contributor/ContributorListItem.vue +35 -35
  48. package/app/components/main/MainBitranContent.vue +41 -0
  49. package/app/components/main/{utils/Breadcrumb.vue → MainBreadcrumb.vue} +67 -74
  50. package/app/components/main/MainDescription.vue +24 -0
  51. package/app/components/main/{utils/ContentTitle.vue → MainTitle.vue} +72 -63
  52. package/app/components/main/content/ContentBreadcrumb.vue +28 -0
  53. package/app/components/main/{utils → content}/ContentDecoration.vue +29 -29
  54. package/app/components/main/{utils → content}/ContentPopover.vue +188 -188
  55. package/app/components/main/{utils → content}/ContentPopovers.vue +111 -111
  56. package/app/components/main/{utils → content}/ContentReferences.vue +70 -70
  57. package/app/components/main/{utils → content}/ContentSection.vue +45 -45
  58. package/app/components/main/{utils → content}/reference/ReferenceGroup.vue +38 -38
  59. package/app/components/main/{utils → content}/reference/ReferenceItem.vue +70 -70
  60. package/app/components/main/{utils → content}/reference/ReferenceSource.vue +120 -120
  61. package/app/components/main/topic/MainTopic.vue +83 -88
  62. package/app/components/main/topic/TopicPartSwitch.vue +124 -118
  63. package/app/components/preview/Preview.vue +186 -186
  64. package/app/components/preview/PreviewDisplay.vue +139 -139
  65. package/app/components/preview/PreviewFooterAction.vue +73 -73
  66. package/app/components/preview/PreviewLoading.vue +14 -14
  67. package/app/components/preview/PreviewScreen.vue +141 -141
  68. package/app/components/preview/display/Alert.vue +50 -50
  69. package/app/components/preview/display/Custom.vue +18 -18
  70. package/app/components/preview/display/GenericLink.vue +48 -48
  71. package/app/components/preview/display/PageLink.vue +22 -22
  72. package/app/components/preview/display/Unique.vue +46 -55
  73. package/app/components/transition/Fade.vue +19 -19
  74. package/app/components/tree/TreeContainer.vue +11 -11
  75. package/app/components/tree/TreeItem.vue +89 -89
  76. package/app/composables/adsAllowed.ts +11 -0
  77. package/app/composables/bitran.ts +108 -108
  78. package/app/composables/bitranLocation.ts +7 -7
  79. package/app/composables/contentData.ts +38 -38
  80. package/app/composables/contentPage.ts +168 -158
  81. package/app/composables/contentRoute.ts +45 -45
  82. package/app/composables/darkMagic.ts +24 -24
  83. package/app/composables/externalApi.ts +69 -69
  84. package/app/composables/favicon.ts +8 -8
  85. package/app/composables/formatText.ts +99 -99
  86. package/app/composables/majorPane.ts +61 -60
  87. package/app/composables/phrases.ts +77 -65
  88. package/app/composables/theme.ts +29 -29
  89. package/app/composables/url.ts +33 -33
  90. package/app/pages/_test/preview.vue +110 -110
  91. package/app/pages/article/[...articleId].vue +3 -3
  92. package/app/pages/book/[...bookId].vue +42 -47
  93. package/app/pages/contributor/[contributorId].vue +225 -0
  94. package/app/pages/contributors.vue +183 -0
  95. package/app/pages/group/[...groupId].vue +58 -66
  96. package/app/pages/index.vue +32 -32
  97. package/app/pages/practice/[...practice].vue +3 -3
  98. package/app/pages/summary/[...summaryId].vue +3 -3
  99. package/app/plugins/analytics.ts +8 -0
  100. package/app/plugins/prerender.server.ts +22 -22
  101. package/app/scripts/_immediate.js +9 -9
  102. package/app/scripts/aside/index.ts +59 -59
  103. package/app/scripts/aside/major/nav.ts +26 -26
  104. package/app/scripts/aside/minor/state.ts +37 -37
  105. package/app/scripts/aside/minor/topic.ts +3 -3
  106. package/app/scripts/flag.ts +28 -28
  107. package/app/scripts/og.ts +27 -27
  108. package/app/scripts/preview/build.ts +76 -76
  109. package/app/scripts/preview/data/alert.ts +19 -19
  110. package/app/scripts/preview/data/custom.ts +8 -8
  111. package/app/scripts/preview/data/genericLink.ts +24 -24
  112. package/app/scripts/preview/data/pageLink.ts +23 -23
  113. package/app/scripts/preview/data/unique.ts +69 -72
  114. package/app/scripts/preview/data.ts +24 -24
  115. package/app/scripts/preview/display.ts +37 -37
  116. package/app/scripts/preview/footer.ts +9 -9
  117. package/app/scripts/preview/request.ts +51 -51
  118. package/app/scripts/preview/state.ts +63 -63
  119. package/app/styles/_immediate.css +7 -7
  120. package/app/styles/_util.scss +43 -43
  121. package/app/styles/_utils.scss +44 -44
  122. package/app/styles/app.scss +91 -91
  123. package/app/styles/def/_bp.scss +27 -27
  124. package/app/styles/def/_size.scss +7 -7
  125. package/app/styles/def/_z.scss +5 -5
  126. package/app/styles/normalize.scss +49 -49
  127. package/app/styles/partials/_darkMagic.scss +5 -5
  128. package/app/styles/partials/_fnav.scss +15 -15
  129. package/app/styles/partials/_preview.scss +5 -5
  130. package/bin/erudit.mjs +2 -2
  131. package/const.ts +4 -4
  132. package/globalPath.ts +21 -21
  133. package/globals/bitran.ts +1 -1
  134. package/globals/content.ts +27 -27
  135. package/globals/contributor.ts +5 -5
  136. package/globals/erudit.ts +5 -5
  137. package/globals/register.ts +18 -18
  138. package/languages/en.ts +103 -94
  139. package/languages/ru.ts +107 -98
  140. package/module/bitran.ts +66 -66
  141. package/module/config.ts +35 -35
  142. package/module/imports.ts +67 -67
  143. package/module/index.ts +47 -47
  144. package/module/logger.ts +10 -10
  145. package/module/paths.ts +22 -22
  146. package/module/restart.ts +61 -61
  147. package/nuxt.config.ts +131 -131
  148. package/package.json +5 -5
  149. package/server/api/aside/major/nav/bookIds.ts +5 -5
  150. package/server/api/aside/major/nav/bookNav/[...bookId].ts +17 -20
  151. package/server/api/aside/major/nav/global.ts +7 -7
  152. package/server/api/aside/minor/book/[...bookId].ts +18 -0
  153. package/server/api/aside/minor/contributor/[contributorId].ts +18 -0
  154. package/server/api/aside/minor/group/[...groupId].ts +18 -0
  155. package/server/api/aside/minor/news.ts +7 -7
  156. package/server/api/aside/minor/topic.ts +36 -0
  157. package/server/api/bitran/content/[...location].ts +13 -10
  158. package/server/api/bitran/toc/[...location].ts +9 -9
  159. package/server/api/content/data.ts +75 -75
  160. package/server/api/contributor/count.ts +6 -6
  161. package/server/api/contributor/list.ts +44 -0
  162. package/server/api/contributor/page/[contributorId].ts +14 -0
  163. package/server/api/fake/content.ts +11 -11
  164. package/server/api/fake/shared/languages.ts +12 -12
  165. package/server/api/language/functions.ts +12 -12
  166. package/server/api/language/phrase/[phraseId].ts +19 -19
  167. package/server/api/language/phraseIds.ts +8 -8
  168. package/server/api/prerender.ts +19 -120
  169. package/server/api/preview/page/[...parts].ts +78 -78
  170. package/server/api/preview/unique/[...location].ts +48 -61
  171. package/server/plugin/bitran/content.ts +252 -190
  172. package/server/plugin/bitran/elements/include.ts +229 -229
  173. package/server/plugin/bitran/location.ts +43 -39
  174. package/server/plugin/bitran/toc.ts +94 -94
  175. package/server/plugin/bitran/transpiler.ts +18 -18
  176. package/server/plugin/build/close.ts +12 -12
  177. package/server/plugin/build/jobs/content/builderArgs.ts +8 -8
  178. package/server/plugin/build/jobs/content/generic.ts +191 -191
  179. package/server/plugin/build/jobs/content/parse.ts +113 -113
  180. package/server/plugin/build/jobs/content/path.ts +6 -6
  181. package/server/plugin/build/jobs/content/type/book.ts +9 -9
  182. package/server/plugin/build/jobs/content/type/group.ts +37 -37
  183. package/server/plugin/build/jobs/content/type/topic.ts +36 -36
  184. package/server/plugin/build/jobs/contributors.ts +69 -66
  185. package/server/plugin/build/jobs/language.ts +36 -36
  186. package/server/plugin/build/jobs/nav.ts +345 -345
  187. package/server/plugin/build/process.ts +32 -32
  188. package/server/plugin/build/rebuild.ts +66 -66
  189. package/server/plugin/build/setup.ts +19 -19
  190. package/server/plugin/content/context.ts +119 -119
  191. package/server/plugin/db/entities/Book.ts +7 -7
  192. package/server/plugin/db/entities/Content.ts +45 -45
  193. package/server/plugin/db/entities/Contribution.ts +10 -10
  194. package/server/plugin/db/entities/Contributor.ts +25 -16
  195. package/server/plugin/db/entities/File.ts +10 -10
  196. package/server/plugin/db/entities/Group.ts +14 -14
  197. package/server/plugin/db/entities/Hash.ts +15 -15
  198. package/server/plugin/db/entities/Topic.ts +20 -20
  199. package/server/plugin/db/entities/Unique.ts +21 -21
  200. package/server/plugin/db/reset.ts +12 -12
  201. package/server/plugin/db/setup.ts +49 -49
  202. package/server/plugin/global.ts +16 -16
  203. package/server/plugin/importer.ts +16 -16
  204. package/server/plugin/index.ts +9 -9
  205. package/server/plugin/logger.ts +23 -23
  206. package/server/plugin/nav/node.ts +27 -27
  207. package/server/plugin/nav/utils.ts +179 -175
  208. package/server/plugin/repository/asideMinor.ts +51 -0
  209. package/server/plugin/repository/book.ts +39 -21
  210. package/server/plugin/repository/content.ts +240 -240
  211. package/server/plugin/repository/contentId.ts +40 -40
  212. package/server/plugin/repository/contributor.ts +98 -8
  213. package/server/plugin/repository/file.ts +10 -10
  214. package/server/plugin/repository/frontNav.ts +145 -145
  215. package/server/plugin/repository/topic.ts +35 -35
  216. package/server/tsconfig.json +9 -9
  217. package/shared/aside/minor.ts +55 -51
  218. package/shared/asset.ts +22 -22
  219. package/shared/bitran/content.ts +9 -0
  220. package/shared/bitran/contentId.ts +56 -56
  221. package/shared/bitran/toc.ts +8 -8
  222. package/shared/breadcrumb.ts +7 -0
  223. package/shared/content/bookId.ts +12 -12
  224. package/shared/content/context.ts +9 -9
  225. package/shared/content/data/base.ts +32 -32
  226. package/shared/content/data/index.ts +5 -5
  227. package/shared/content/data/type/book.ts +5 -5
  228. package/shared/content/data/type/group.ts +6 -6
  229. package/shared/content/data/type/topic.ts +11 -11
  230. package/shared/content/previousNext.ts +9 -9
  231. package/shared/contributor.ts +33 -5
  232. package/shared/frontNav.ts +41 -41
  233. package/shared/icons.ts +38 -38
  234. package/shared/image.ts +5 -5
  235. package/shared/link.ts +28 -28
  236. package/shared/popover.ts +8 -8
  237. package/shared/types/language.ts +79 -74
  238. package/shared/utils/objectsEqual.ts +4 -4
  239. package/shared/utils/stringColor.ts +9 -9
  240. package/test/contentId.test.ts +91 -91
  241. package/tsconfig.json +8 -8
  242. package/utils/contentPath.ts +67 -67
  243. package/utils/slash.ts +11 -11
  244. package/utils/stress.ts +9 -9
  245. package/app/components/aside/minor/AsideMinorContributor.vue +0 -5
  246. package/app/components/main/utils/ContentDescription.vue +0 -19
  247. package/app/composables/bitranContent.ts +0 -92
  248. package/app/pages/members.vue +0 -6
  249. package/server/api/aside/minor/path.ts +0 -82
  250. package/shared/bitran/stringContent.ts +0 -6
@@ -1,345 +1,345 @@
1
- import { globSync } from 'glob';
2
- import chalk from 'chalk';
3
- import { readFileSync, writeFileSync, existsSync } from 'fs';
4
- import { join } from 'path';
5
- import {
6
- contentTypes,
7
- topicParts,
8
- type ContentType,
9
- } from '@erudit-js/cog/schema';
10
-
11
- import { PROJECT_DIR } from '#erudit/globalPaths';
12
- import { stress } from '@erudit/utils/stress';
13
- import { debug, logger } from '@server/logger';
14
- import {
15
- createRootNode,
16
- isRootNode,
17
- type NavNode,
18
- type RootNavNode,
19
- } from '@server/nav/node';
20
- import { ERUDIT_SERVER } from '@server/global';
21
- import { jiti } from '@server/importer';
22
-
23
- type Ids = Record<string, string>;
24
- let fullIds: Ids;
25
-
26
- const nodePathRegexp = new RegExp(
27
- `(?<pos>\\d+)(?<sep>-|\\+)(?<id>[\\w-]+)\\/(?<type>${contentTypes.join('|')})\\..*`,
28
- );
29
-
30
- const contentFilter = createContentFilter();
31
-
32
- export async function buildNav() {
33
- debug.start('Building navigation tree...');
34
-
35
- fullIds = {};
36
-
37
- const rootNode = createRootNode();
38
- rootNode.children = (await scanChildNodes(rootNode, false)).children;
39
- ERUDIT_SERVER.NAV = rootNode.children ? rootNode : undefined;
40
-
41
- const nodeCount = Object.values(fullIds).length;
42
-
43
- if (nodeCount === 0) {
44
- logger.warn('No content nodes detected! The site will be empty!');
45
- } else {
46
- if (ERUDIT_SERVER.CONFIG?.debug?.log) debugPrintNav(rootNode);
47
-
48
- writeCompilerOptionsPaths();
49
- writeJitiAliases();
50
-
51
- logger.success(
52
- 'Navigation tree built successfully!',
53
- chalk.dim(`(${nodeCount})`),
54
- );
55
- }
56
- }
57
-
58
- async function scanChildNodes(
59
- parent: NavNode | RootNavNode,
60
- insideBook: boolean,
61
- ): Promise<{ children: NavNode[] | undefined; newIds: Ids }> {
62
- const currentFsPath = isRootNode(parent) ? '' : parent.fsPath + '/';
63
-
64
- const nodeFsPaths = globSync(
65
- `${currentFsPath}*/{${contentTypes.join(',')}}.{ts,js}`,
66
- {
67
- cwd: PROJECT_DIR + '/content',
68
- posix: true,
69
- },
70
- ).sort();
71
-
72
- let newIds: Ids = {};
73
- const children: NavNode[] = [];
74
-
75
- for (const nodeFsPath of nodeFsPaths) {
76
- const pathParts:
77
- | {
78
- pos: string;
79
- sep: string;
80
- id: string;
81
- type: ContentType;
82
- }
83
- | undefined = nodeFsPath.match(nodePathRegexp)?.groups as any;
84
-
85
- if (!pathParts) continue; // Wrong path pattern
86
-
87
- const nodePath = nodeFsPath.split('/').slice(0, -1).join('/');
88
-
89
- if (pathParts.type === 'book' && insideBook) {
90
- logger.warn(
91
- 'Books inside books are not allowed!\n',
92
- `Skipping ${stress(nodePath)} nav node!`,
93
- );
94
- continue;
95
- }
96
-
97
- const parentId = isRootNode(parent)
98
- ? ''
99
- : parent.skip
100
- ? parent.fullId.split('/').slice(0, -1).join('/')
101
- : parent.fullId;
102
-
103
- // Regular id might not include parent id part if it is skipped
104
- const id = parentId ? `${parentId}/${pathParts.id}` : pathParts.id;
105
-
106
- if (fullIds[id]) {
107
- logger.warn(
108
- `Nav node ${stress(id)} ID collision!\n\n`,
109
- `This ID belongs to ${stress(fullIds[id], chalk.greenBright)} nav node.\n`,
110
- `Nav node ${stress(nodePath, chalk.redBright)} tries to use the same ID!\n`,
111
- 'Skipping nav node which causes collision!',
112
- );
113
- continue;
114
- }
115
-
116
- // Full id always includes parent id
117
- const fullId = isRootNode(parent)
118
- ? pathParts.id
119
- : `${parent.fullId}/${pathParts.id}`;
120
-
121
- // Short id skips parent ids for nodes with skip=true, except current node
122
- const shortId = isRootNode(parent)
123
- ? pathParts.id
124
- : (() => {
125
- // Traverse up, skipping parent ids where skip=true
126
- let ids: string[] = [];
127
- let p: NavNode | RootNavNode | undefined = parent;
128
- while (p && !isRootNode(p)) {
129
- if (!p.skip) ids.unshift(p.idPart);
130
- p = p.parent;
131
- }
132
- ids.push(pathParts.id);
133
- return ids.join('/');
134
- })();
135
-
136
- const shouldSkip = (() => {
137
- const targets = [
138
- ...contentFilter.cliContentTargets,
139
- ...(ERUDIT_SERVER.CONFIG?.contentTargets || []),
140
- ];
141
-
142
- // If there are no filters, allow all nodes
143
- if (targets.length === 0) {
144
- return false;
145
- }
146
-
147
- // If the node passes at least one filter, keep it
148
- for (const target of targets) {
149
- if (contentFilter.strFilter(fullId, target)) {
150
- return false;
151
- }
152
- }
153
-
154
- // Node failed all filters, skip it
155
- return true;
156
- })();
157
-
158
- if (shouldSkip) continue;
159
-
160
- const skip = pathParts.sep === '+';
161
-
162
- const childNode: NavNode = {
163
- parent,
164
- type: pathParts.type,
165
- fsPath: nodePath,
166
- idPart: pathParts.id,
167
- fullId,
168
- shortId,
169
- skip,
170
- };
171
-
172
- if (!validNode(childNode)) continue;
173
-
174
- fullIds[id] = nodePath;
175
- newIds[id] = nodePath;
176
-
177
- const scanResult = await scanChildNodes(
178
- childNode,
179
- insideBook || childNode.type === 'book',
180
- );
181
-
182
- if (
183
- ['book', 'group'].includes(childNode.type) &&
184
- !scanResult.children
185
- ) {
186
- delete fullIds[id];
187
- delete newIds[id];
188
- for (const childNewId of Object.keys(scanResult.newIds))
189
- delete fullIds[childNewId];
190
- continue;
191
- }
192
-
193
- if (childNode.type === 'book') {
194
- ERUDIT_SERVER.NAV_BOOKS ||= {};
195
- ERUDIT_SERVER.NAV_BOOKS[id] = childNode;
196
- }
197
-
198
- childNode.children = scanResult.children;
199
- children.push(childNode);
200
- }
201
-
202
- return {
203
- children: children.length > 0 ? children : undefined,
204
- newIds,
205
- };
206
- }
207
-
208
- function createContentFilter() {
209
- const cliContentTargets = (() => {
210
- try {
211
- return JSON.parse(
212
- readFileSync(PROJECT_DIR + '/.erudit/targets.json', 'utf-8'),
213
- );
214
- } catch (error) {}
215
-
216
- return [];
217
- })();
218
-
219
- const strFilter = (fullId: string, filterItem: string) => {
220
- if (fullId.startsWith(filterItem) || filterItem.search(fullId) === 0) {
221
- return true;
222
- }
223
-
224
- return false;
225
- };
226
-
227
- return {
228
- cliContentTargets,
229
- strFilter,
230
- };
231
- }
232
-
233
- function validNode(node: NavNode): boolean {
234
- switch (node.type) {
235
- case 'topic':
236
- const partPaths = globSync(
237
- PROJECT_DIR +
238
- `/content/${node.fsPath}` +
239
- `/{${topicParts.join(',')}}.bi`,
240
- );
241
- if (partPaths.length === 0) return false; // Topic is empty
242
- break;
243
- case 'book':
244
- if (node.skip) {
245
- logger.warn(
246
- `Books can't skip their ID part!\n`,
247
- `Skipping ${stress(node.fsPath)} nav node!`,
248
- );
249
- return false;
250
- }
251
- break;
252
- }
253
-
254
- return true;
255
- }
256
-
257
- function debugPrintNav(node: RootNavNode) {
258
- const logNode = (node: NavNode | RootNavNode, indent: number) => {
259
- console.log(
260
- isRootNode(node)
261
- ? chalk.dim('#root')
262
- : `${' '.repeat(indent)}${node.idPart} ${chalk.dim(`[${node.type}${node.skip ? ', ' + chalk.yellow('skip') : ''}]`)}`,
263
- );
264
-
265
- if (node.children)
266
- for (const child of node.children) logNode(child, indent + 1);
267
- };
268
-
269
- logNode(node, 0);
270
- }
271
-
272
- function writeCompilerOptionsPaths() {
273
- const tsconfigPath = `${PROJECT_DIR}/.erudit/tsconfig.json`;
274
-
275
- try {
276
- let tsconfig: any = {};
277
-
278
- if (existsSync(tsconfigPath)) {
279
- try {
280
- tsconfig = JSON.parse(readFileSync(tsconfigPath, 'utf8'));
281
- } catch (err) {
282
- logger.warn(
283
- `Error parsing ${tsconfigPath}, creating a new one.`,
284
- );
285
- }
286
- }
287
-
288
- tsconfig.compilerOptions = tsconfig.compilerOptions || {};
289
- tsconfig.compilerOptions.paths = tsconfig.compilerOptions.paths || {};
290
-
291
- const paths = tsconfig.compilerOptions.paths;
292
-
293
- Object.keys(paths).forEach((key) => {
294
- if (key === '#content/*') {
295
- return;
296
- }
297
-
298
- if (key.startsWith('#content/')) {
299
- delete paths[key];
300
- }
301
- });
302
-
303
- Object.entries(fullIds).forEach(([id, fsPath]) => {
304
- const pathKey = `#content/${id}/*`;
305
- paths[pathKey] = [`../content/${fsPath}/*`];
306
- });
307
-
308
- writeFileSync(tsconfigPath, JSON.stringify(tsconfig, null, 4), 'utf8');
309
- logger.success(`Updated tsconfig content paths!`);
310
- } catch (_error) {
311
- logger.error(`Failed to write compiler paths: ${_error}`);
312
- }
313
- }
314
-
315
- function writeJitiAliases() {
316
- try {
317
- if (!jiti.options) {
318
- jiti.options = {};
319
- }
320
-
321
- if (!jiti.options.alias) {
322
- jiti.options.alias = {};
323
- }
324
-
325
- const aliases = jiti.options.alias;
326
-
327
- Object.keys(aliases).forEach((key) => {
328
- if (key === '#content/*') {
329
- return;
330
- }
331
-
332
- if (key.startsWith('#content/')) {
333
- delete aliases[key];
334
- }
335
- });
336
-
337
- Object.entries(fullIds).forEach(([id, fsPath]) => {
338
- aliases[`#content/${id}`] = `${PROJECT_DIR}/content/${fsPath}`;
339
- });
340
-
341
- logger.success('Updated jiti content aliases!');
342
- } catch (_error) {
343
- logger.error(`Failed to write jiti aliases: ${_error}`);
344
- }
345
- }
1
+ import { globSync } from 'glob';
2
+ import chalk from 'chalk';
3
+ import { readFileSync, writeFileSync, existsSync } from 'fs';
4
+ import { join } from 'path';
5
+ import {
6
+ contentTypes,
7
+ topicParts,
8
+ type ContentType,
9
+ } from '@erudit-js/cog/schema';
10
+
11
+ import { PROJECT_DIR } from '#erudit/globalPaths';
12
+ import { stress } from '@erudit/utils/stress';
13
+ import { debug, logger } from '@server/logger';
14
+ import {
15
+ createRootNode,
16
+ isRootNode,
17
+ type NavNode,
18
+ type RootNavNode,
19
+ } from '@server/nav/node';
20
+ import { ERUDIT_SERVER } from '@server/global';
21
+ import { jiti } from '@server/importer';
22
+
23
+ type Ids = Record<string, string>;
24
+ let fullIds: Ids;
25
+
26
+ const nodePathRegexp = new RegExp(
27
+ `(?<pos>\\d+)(?<sep>-|\\+)(?<id>[\\w-]+)\\/(?<type>${contentTypes.join('|')})\\..*`,
28
+ );
29
+
30
+ const contentFilter = createContentFilter();
31
+
32
+ export async function buildNav() {
33
+ debug.start('Building navigation tree...');
34
+
35
+ fullIds = {};
36
+
37
+ const rootNode = createRootNode();
38
+ rootNode.children = (await scanChildNodes(rootNode, false)).children;
39
+ ERUDIT_SERVER.NAV = rootNode.children ? rootNode : undefined;
40
+
41
+ const nodeCount = Object.values(fullIds).length;
42
+
43
+ if (nodeCount === 0) {
44
+ logger.warn('No content nodes detected! The site will be empty!');
45
+ } else {
46
+ if (ERUDIT_SERVER.CONFIG?.debug?.log) debugPrintNav(rootNode);
47
+
48
+ writeCompilerOptionsPaths();
49
+ writeJitiAliases();
50
+
51
+ logger.success(
52
+ 'Navigation tree built successfully!',
53
+ chalk.dim(`(${nodeCount})`),
54
+ );
55
+ }
56
+ }
57
+
58
+ async function scanChildNodes(
59
+ parent: NavNode | RootNavNode,
60
+ insideBook: boolean,
61
+ ): Promise<{ children: NavNode[] | undefined; newIds: Ids }> {
62
+ const currentFsPath = isRootNode(parent) ? '' : parent.fsPath + '/';
63
+
64
+ const nodeFsPaths = globSync(
65
+ `${currentFsPath}*/{${contentTypes.join(',')}}.{ts,js}`,
66
+ {
67
+ cwd: PROJECT_DIR + '/content',
68
+ posix: true,
69
+ },
70
+ ).sort();
71
+
72
+ let newIds: Ids = {};
73
+ const children: NavNode[] = [];
74
+
75
+ for (const nodeFsPath of nodeFsPaths) {
76
+ const pathParts:
77
+ | {
78
+ pos: string;
79
+ sep: string;
80
+ id: string;
81
+ type: ContentType;
82
+ }
83
+ | undefined = nodeFsPath.match(nodePathRegexp)?.groups as any;
84
+
85
+ if (!pathParts) continue; // Wrong path pattern
86
+
87
+ const nodePath = nodeFsPath.split('/').slice(0, -1).join('/');
88
+
89
+ if (pathParts.type === 'book' && insideBook) {
90
+ logger.warn(
91
+ 'Books inside books are not allowed!\n',
92
+ `Skipping ${stress(nodePath)} nav node!`,
93
+ );
94
+ continue;
95
+ }
96
+
97
+ const parentId = isRootNode(parent)
98
+ ? ''
99
+ : parent.skip
100
+ ? parent.fullId.split('/').slice(0, -1).join('/')
101
+ : parent.fullId;
102
+
103
+ // Regular id might not include parent id part if it is skipped
104
+ const id = parentId ? `${parentId}/${pathParts.id}` : pathParts.id;
105
+
106
+ if (fullIds[id]) {
107
+ logger.warn(
108
+ `Nav node ${stress(id)} ID collision!\n\n`,
109
+ `This ID belongs to ${stress(fullIds[id], chalk.greenBright)} nav node.\n`,
110
+ `Nav node ${stress(nodePath, chalk.redBright)} tries to use the same ID!\n`,
111
+ 'Skipping nav node which causes collision!',
112
+ );
113
+ continue;
114
+ }
115
+
116
+ // Full id always includes parent id
117
+ const fullId = isRootNode(parent)
118
+ ? pathParts.id
119
+ : `${parent.fullId}/${pathParts.id}`;
120
+
121
+ // Short id skips parent ids for nodes with skip=true, except current node
122
+ const shortId = isRootNode(parent)
123
+ ? pathParts.id
124
+ : (() => {
125
+ // Traverse up, skipping parent ids where skip=true
126
+ let ids: string[] = [];
127
+ let p: NavNode | RootNavNode | undefined = parent;
128
+ while (p && !isRootNode(p)) {
129
+ if (!p.skip) ids.unshift(p.idPart);
130
+ p = p.parent;
131
+ }
132
+ ids.push(pathParts.id);
133
+ return ids.join('/');
134
+ })();
135
+
136
+ const shouldSkip = (() => {
137
+ const targets = [
138
+ ...contentFilter.cliContentTargets,
139
+ ...(ERUDIT_SERVER.CONFIG?.contentTargets || []),
140
+ ];
141
+
142
+ // If there are no filters, allow all nodes
143
+ if (targets.length === 0) {
144
+ return false;
145
+ }
146
+
147
+ // If the node passes at least one filter, keep it
148
+ for (const target of targets) {
149
+ if (contentFilter.strFilter(fullId, target)) {
150
+ return false;
151
+ }
152
+ }
153
+
154
+ // Node failed all filters, skip it
155
+ return true;
156
+ })();
157
+
158
+ if (shouldSkip) continue;
159
+
160
+ const skip = pathParts.sep === '+';
161
+
162
+ const childNode: NavNode = {
163
+ parent,
164
+ type: pathParts.type,
165
+ fsPath: nodePath,
166
+ idPart: pathParts.id,
167
+ fullId,
168
+ shortId,
169
+ skip,
170
+ };
171
+
172
+ if (!validNode(childNode)) continue;
173
+
174
+ fullIds[fullId] = nodePath;
175
+ newIds[fullId] = nodePath;
176
+
177
+ const scanResult = await scanChildNodes(
178
+ childNode,
179
+ insideBook || childNode.type === 'book',
180
+ );
181
+
182
+ if (
183
+ ['book', 'group'].includes(childNode.type) &&
184
+ !scanResult.children
185
+ ) {
186
+ delete fullIds[fullId];
187
+ delete newIds[fullId];
188
+ for (const childNewId of Object.keys(scanResult.newIds))
189
+ delete fullIds[childNewId];
190
+ continue;
191
+ }
192
+
193
+ if (childNode.type === 'book') {
194
+ ERUDIT_SERVER.NAV_BOOKS ||= {};
195
+ ERUDIT_SERVER.NAV_BOOKS[fullId] = childNode;
196
+ }
197
+
198
+ childNode.children = scanResult.children;
199
+ children.push(childNode);
200
+ }
201
+
202
+ return {
203
+ children: children.length > 0 ? children : undefined,
204
+ newIds,
205
+ };
206
+ }
207
+
208
+ function createContentFilter() {
209
+ const cliContentTargets = (() => {
210
+ try {
211
+ return JSON.parse(
212
+ readFileSync(PROJECT_DIR + '/.erudit/targets.json', 'utf-8'),
213
+ );
214
+ } catch (error) {}
215
+
216
+ return [];
217
+ })();
218
+
219
+ const strFilter = (fullId: string, filterItem: string) => {
220
+ if (fullId.startsWith(filterItem) || filterItem.search(fullId) === 0) {
221
+ return true;
222
+ }
223
+
224
+ return false;
225
+ };
226
+
227
+ return {
228
+ cliContentTargets,
229
+ strFilter,
230
+ };
231
+ }
232
+
233
+ function validNode(node: NavNode): boolean {
234
+ switch (node.type) {
235
+ case 'topic':
236
+ const partPaths = globSync(
237
+ PROJECT_DIR +
238
+ `/content/${node.fsPath}` +
239
+ `/{${topicParts.join(',')}}.bi`,
240
+ );
241
+ if (partPaths.length === 0) return false; // Topic is empty
242
+ break;
243
+ case 'book':
244
+ if (node.skip) {
245
+ logger.warn(
246
+ `Books can't skip their ID part!\n`,
247
+ `Skipping ${stress(node.fsPath)} nav node!`,
248
+ );
249
+ return false;
250
+ }
251
+ break;
252
+ }
253
+
254
+ return true;
255
+ }
256
+
257
+ function debugPrintNav(node: RootNavNode) {
258
+ const logNode = (node: NavNode | RootNavNode, indent: number) => {
259
+ console.log(
260
+ isRootNode(node)
261
+ ? chalk.dim('#root')
262
+ : `${' '.repeat(indent)}${node.idPart} ${chalk.dim(`[${node.type}${node.skip ? ', ' + chalk.yellow('skip') : ''}]`)}`,
263
+ );
264
+
265
+ if (node.children)
266
+ for (const child of node.children) logNode(child, indent + 1);
267
+ };
268
+
269
+ logNode(node, 0);
270
+ }
271
+
272
+ function writeCompilerOptionsPaths() {
273
+ const tsconfigPath = `${PROJECT_DIR}/.erudit/tsconfig.json`;
274
+
275
+ try {
276
+ let tsconfig: any = {};
277
+
278
+ if (existsSync(tsconfigPath)) {
279
+ try {
280
+ tsconfig = JSON.parse(readFileSync(tsconfigPath, 'utf8'));
281
+ } catch (err) {
282
+ logger.warn(
283
+ `Error parsing ${tsconfigPath}, creating a new one.`,
284
+ );
285
+ }
286
+ }
287
+
288
+ tsconfig.compilerOptions = tsconfig.compilerOptions || {};
289
+ tsconfig.compilerOptions.paths = tsconfig.compilerOptions.paths || {};
290
+
291
+ const paths = tsconfig.compilerOptions.paths;
292
+
293
+ Object.keys(paths).forEach((key) => {
294
+ if (key === '#content/*') {
295
+ return;
296
+ }
297
+
298
+ if (key.startsWith('#content/')) {
299
+ delete paths[key];
300
+ }
301
+ });
302
+
303
+ Object.entries(fullIds).forEach(([id, fsPath]) => {
304
+ const pathKey = `#content/${id}/*`;
305
+ paths[pathKey] = [`../content/${fsPath}/*`];
306
+ });
307
+
308
+ writeFileSync(tsconfigPath, JSON.stringify(tsconfig, null, 4), 'utf8');
309
+ logger.success(`Updated tsconfig content paths!`);
310
+ } catch (_error) {
311
+ logger.error(`Failed to write compiler paths: ${_error}`);
312
+ }
313
+ }
314
+
315
+ function writeJitiAliases() {
316
+ try {
317
+ if (!jiti.options) {
318
+ jiti.options = {};
319
+ }
320
+
321
+ if (!jiti.options.alias) {
322
+ jiti.options.alias = {};
323
+ }
324
+
325
+ const aliases = jiti.options.alias;
326
+
327
+ Object.keys(aliases).forEach((key) => {
328
+ if (key === '#content/*') {
329
+ return;
330
+ }
331
+
332
+ if (key.startsWith('#content/')) {
333
+ delete aliases[key];
334
+ }
335
+ });
336
+
337
+ Object.entries(fullIds).forEach(([id, fsPath]) => {
338
+ aliases[`#content/${id}`] = `${PROJECT_DIR}/content/${fsPath}`;
339
+ });
340
+
341
+ logger.success('Updated jiti content aliases!');
342
+ } catch (_error) {
343
+ logger.error(`Failed to write jiti aliases: ${_error}`);
344
+ }
345
+ }