coralite-plugin-aggregation 0.1.2 → 0.2.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.
package/README.md CHANGED
@@ -58,7 +58,7 @@ Create a file like `coralite-posts.html` to define your aggregation logic and re
58
58
  </template>
59
59
 
60
60
  <script type="module">
61
- import { defineComponent, aggregation } from 'coralite'
61
+ import { defineComponent, aggregation } from 'coralite/plugins'
62
62
 
63
63
  export default defineComponent({
64
64
  tokens: {
@@ -133,7 +133,7 @@ Define a `<template>` element for rendering individual items:
133
133
  </template>
134
134
 
135
135
  <script type="module">
136
- import { defineComponent } from 'coralite'
136
+ import { defineComponent } from 'coralite/plugins'
137
137
 
138
138
  export default defineComponent({
139
139
  tokens: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "coralite-plugin-aggregation",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "Build database free coralite websites",
5
5
  "scripts": {
6
6
  "build": "coralite -t tests/fixtures/templates -p tests/fixtures/pages -o dist",
@@ -33,12 +33,12 @@
33
33
  }
34
34
  },
35
35
  "license": "AGPL-3.0-only",
36
- "packageManager": "pnpm@10.12.1",
36
+ "packageManager": "pnpm@10.13.1",
37
37
  "devDependencies": {
38
38
  "@playwright/test": "^1.52.0",
39
39
  "@stylistic/eslint-plugin-js": "^4.2.0",
40
40
  "@stylistic/eslint-plugin-plus": "^4.2.0",
41
- "coralite": "^0.11.1",
41
+ "coralite": "^0.12.0",
42
42
  "sirv-cli": "^3.0.1"
43
43
  },
44
44
  "dependencies": {
@@ -46,6 +46,6 @@
46
46
  "htmlparser2": "^10.0.0"
47
47
  },
48
48
  "peerDependencies": {
49
- "coralite": "^0.11.2"
49
+ "coralite": "^0.12.0"
50
50
  }
51
51
  }
package/src/index.js CHANGED
@@ -1,12 +1,6 @@
1
1
  import { join } from 'node:path'
2
2
  import { existsSync } from 'node:fs'
3
- import { Parser } from 'htmlparser2'
4
- import {
5
- getHtmlFiles,
6
- createElement,
7
- createTextNode,
8
- createPlugin,
9
- } from 'coralite/utils'
3
+ import { getHtmlFiles, createPlugin, } from 'coralite/utils'
10
4
 
11
5
  /**
12
6
  * @import {CoraliteAnyNode, CoraliteCollectionItem, CoraliteContentNode, CoraliteDocumentRoot} from 'coralite/types'
@@ -79,7 +73,7 @@ export default createPlugin({
79
73
  let endIndex = pages.length
80
74
  let paginationOffset = context.values.pagination_offset
81
75
 
82
- // Sort results based on custom sort function
76
+ // sort results based on custom sort function
83
77
  if (typeof options.sort === 'function') {
84
78
  pages.sort((a, b) => {
85
79
  const metaA = a.result.values
@@ -97,7 +91,7 @@ export default createPlugin({
97
91
  const metadata = page.result.values
98
92
  let keepItem = false
99
93
 
100
- // Process metadata and populate token values for rendering
94
+ // process metadata and populate token values for rendering
101
95
  for (const key in metadata) {
102
96
  if (Object.prototype.hasOwnProperty.call(metadata, key)) {
103
97
  const data = metadata[key]
@@ -110,7 +104,7 @@ export default createPlugin({
110
104
  }
111
105
  }
112
106
  } else {
113
- // Handle single metadata item
107
+ // handle single metadata item
114
108
  if (!keepItem) {
115
109
  keepItem = options.filter({
116
110
  name: key,
@@ -130,7 +124,7 @@ export default createPlugin({
130
124
  endIndex = pages.length
131
125
  }
132
126
 
133
- // Apply page offset
127
+ // apply page offset
134
128
  if (Object.prototype.hasOwnProperty.call(options, 'offset') || paginationOffset != null) {
135
129
  let offset = paginationOffset || options.offset
136
130
 
@@ -192,6 +186,7 @@ export default createPlugin({
192
186
  }
193
187
  }
194
188
 
189
+ // process pagination
195
190
  if (options.pagination) {
196
191
  const pagination = options.pagination
197
192
  const paginationPath = context.values.pagination_path || pagination.path || 'page'
@@ -239,7 +234,7 @@ export default createPlugin({
239
234
  }
240
235
 
241
236
  // add pagination page to render queue
242
- this.addRenderQueue({
237
+ await this.addRenderQueue({
243
238
  values: {
244
239
  pagination_pager_dirname: path.dirname,
245
240
  pagination_pager_index: pageIndex,
@@ -280,22 +275,84 @@ export default createPlugin({
280
275
  values = context.values
281
276
  this.values[contextId] = values
282
277
  }
283
-
278
+
284
279
  const templateId = pagination.template || 'coralite-pagination'
285
280
 
286
- const component = await this.createComponent({
287
- id: templateId,
288
- values,
289
- document: context.document,
290
- contextId: contextId + templateId
291
- })
281
+ if (typeof templateId === 'string') {
282
+ const component = await this.createComponent({
283
+ id: templateId,
284
+ values,
285
+ document: context.document,
286
+ contextId: contextId + templateId
287
+ })
292
288
 
293
- if (typeof component === 'object') {
294
- result = result.concat(component.children)
289
+ if (typeof component === 'object') {
290
+ result = result.concat(component.children)
291
+ }
295
292
  }
296
293
  }
297
294
 
298
295
  return result
299
296
  },
297
+ async onPageCreate ({ elements, values, data }) {
298
+ // loop through all children of the root element to process metadata in <head> tags.
299
+ for (let i = 0; i < elements.root.children.length; i++) {
300
+ const rootNode = elements.root.children[i]
301
+
302
+ // traverse html children to find the head element
303
+ if (rootNode.type === 'tag' && rootNode.name === 'html') {
304
+ for (let i = 0; i < rootNode.children.length; i++) {
305
+ const node = rootNode.children[i];
306
+
307
+ // check if the current node is a <head> tag where metadata is typically found.
308
+ if (node.type === 'tag' && node.name === 'head') {
309
+ // iterate over the children of the head element to locate meta tags or component slots.
310
+ for (let i = 0; i < node.children.length; i++) {
311
+ const element = node.children[i]
312
+
313
+ // if the element is a tag named "meta" with both name and content attributes, store its metadata.
314
+ if (element.type === 'tag') {
315
+ if (element.name === 'meta'
316
+ && element.attribs.name
317
+ && element.attribs.content
318
+ ) {
319
+ values['$' + element.attribs.name] = element.attribs.content
320
+ } else if (element.slots) {
321
+ // process component slots by creating a component dynamically.
322
+ const component = await this.createComponent({
323
+ id: element.name,
324
+ values,
325
+ element,
326
+ document: data.result,
327
+ contextId: data.path.pathname + i + element.name
328
+ })
329
+
330
+ // if the created component returns valid children, iterate over them to extract meta information.
331
+ if (component) {
332
+ for (let i = 0; i < component.children.length; i++) {
333
+ const element = component.children[i];
334
+
335
+ // for each child element in the component's returned HTML,
336
+ // check if it is a meta tag and store its metadata with a '$' prefix.
337
+ if (element.type === 'tag'
338
+ && element.name === 'meta'
339
+ && element.attribs.name
340
+ && element.attribs.content
341
+ ) {
342
+ values['$' + element.attribs.name] = element.attribs.content
343
+ }
344
+ }
345
+ }
346
+ }
347
+ }
348
+ }
349
+
350
+ // once the <head> tag is processed, return to exit the loop.
351
+ return
352
+ }
353
+ }
354
+ }
355
+ }
356
+ },
300
357
  templates: [join(import.meta.dirname, 'templates/coralite-pagination.html')]
301
358
  })
@@ -3,7 +3,8 @@
3
3
  </template>
4
4
 
5
5
  <script type="module">
6
- import { defineComponent, document } from 'coralite'
6
+ import { document } from 'coralite'
7
+ import { defineComponent } from 'coralite/plugins'
7
8
 
8
9
  export default defineComponent({
9
10
  tokens: {