coralite 0.0.0-development

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.
@@ -0,0 +1,38 @@
1
+ /**
2
+ * @import { CoraliteElement, CoraliteTextNode } from '#types'
3
+ */
4
+
5
+ /**
6
+ * These exports are placeholder for types
7
+ * The HTML module code is run in the `parseScript` function in parse.js
8
+ */
9
+
10
+ /**
11
+ * @type {Object.<string, string>}
12
+ */
13
+ export const tokens = {}
14
+
15
+ /**
16
+ * Defines a Coralite component
17
+ *
18
+ * @param {Object} options
19
+ * @param {string} [options.id] - Optional component id, if not defined, the id will be extracted from the first top level element with the id attribute
20
+ * @param {Object.<string, (string | function)>} options.tokens - A map where keys are token names and values are either strings or functions representing the corresponding tokens' content or behavior.
21
+ * @returns {Promise<Object.<string, string>>}
22
+ */
23
+ export async function defineComponent (options) {
24
+ /** @type {Object.<string, string>} */
25
+ return {}
26
+ }
27
+
28
+ /**
29
+ * Aggregates HTML content from specified paths into a single collection of components.
30
+ *
31
+ * @param {Object} options - Configuration object for the aggregation process
32
+ * @param {string} options.componentId - Unique identifier for the component used for each document
33
+ * @param {string} options.path - The path to aggregate, relative to pages directory
34
+ * @returns {Promise<(CoraliteElement | CoraliteTextNode)[]>}
35
+ */
36
+ export async function aggregate (options) {
37
+ return []
38
+ }
@@ -0,0 +1,64 @@
1
+ import { extname, join } from 'node:path'
2
+ import { readdir, readFile } from 'node:fs/promises'
3
+
4
+ /**
5
+ * @import { HTMLData } from '#types'
6
+ */
7
+
8
+ /**
9
+ * Get HTML
10
+ * @param {Object} options - Options for searching HTML files
11
+ * @param {string} options.path - Path to the directory containing HTML files
12
+ * @param {boolean} [options.recursive=false] - Whether to search recursively in subdirectories
13
+ * @param {string[]} [options.exclude=[]] - Files or directories to exclude from search
14
+ * @returns {Promise<HTMLData[]>} Array of HTML file data including parent path, name, and content
15
+ *
16
+ * @example
17
+ * // Example usage:
18
+ * const htmlFiles = await getHTML({
19
+ * path: 'src',
20
+ * recursive: true,
21
+ * exclude: ['index.html', 'subdir/file2.html']
22
+ * })
23
+ */
24
+ export default function getHTML ({ path, recursive = false, exclude = [] }) {
25
+ return new Promise((resolve, reject) => {
26
+ const html = []
27
+
28
+ readdir(path, {
29
+ recursive,
30
+ withFileTypes: true
31
+ })
32
+ .then(files => {
33
+ const promises = []
34
+ for (let i = 0; i < files.length; i++) {
35
+ const file = files[i]
36
+
37
+ if (file.isFile()
38
+ && extname(file.name).toLowerCase() === '.html'
39
+ && !exclude.includes(file.name)
40
+ ) {
41
+ const parentPath = file.parentPath || file.path
42
+
43
+ html.push({
44
+ parentPath,
45
+ name: file.name
46
+ })
47
+
48
+ promises.push(readFile(join(parentPath, file.name), { encoding: 'utf8' }))
49
+ }
50
+ }
51
+
52
+ Promise.all(promises)
53
+ .then(results => {
54
+ for (let i = 0; i < results.length; i++) {
55
+ html[i].content = results[i]
56
+ }
57
+
58
+ resolve(html)
59
+ })
60
+ .catch(error => reject(error))
61
+ })
62
+ .catch(error => reject(error))
63
+ })
64
+ }
@@ -0,0 +1,75 @@
1
+ import { join } from 'node:path'
2
+ import getHTML from './get-html.js'
3
+ import { createComponent, parseHTMLMeta } from './parse.js'
4
+
5
+ /**
6
+ * @import { CoraliteTokenOptions, CoraliteModule, CoraliteDocument } from '#types'
7
+ */
8
+
9
+ /**
10
+ * Aggregates HTML content from specified paths into a single collection of components.
11
+ *
12
+ * @param {Object} options - Configuration object for the aggregation process
13
+ * @param {string} options.path - The path to aggregate, relative to pages directory
14
+ * @param {string} options.componentId - Unique identifier for the component
15
+ * @param {boolean} [options.recursive] - Whether to recursively search subdirectories
16
+ * @param {CoraliteTokenOptions} [options.tokens] - Token configuration options
17
+ * @param {Object.<string, string>} values - Default token values
18
+ * @param {Object.<string, CoraliteModule>} components - Available components library
19
+ * @param {CoraliteDocument} document - Current document being processed
20
+ *
21
+ * @example
22
+ * ```javascript
23
+ * // Aggregating content from pages under 'components' directory into a component with id 'my-component'
24
+ * aggregate({
25
+ * path: 'button',
26
+ * recursive: true,
27
+ * componentId: 'my-component'
28
+ * }, {
29
+ * className: 'btn'
30
+ * }, components, document);
31
+ * ```
32
+ */
33
+ export async function aggregate (options, values, components, document) {
34
+ const pages = await getHTML({
35
+ path: join(document.path.pages, options.path),
36
+ recursive: options.recursive,
37
+ exclude: [document.name]
38
+ })
39
+
40
+ let result = []
41
+
42
+ for (let i = 0; i < pages.length; i++) {
43
+ const page = pages[i]
44
+ const meta = parseHTMLMeta(page.content)
45
+ const pageValues = Object.assign({}, values)
46
+
47
+ for (const key in meta) {
48
+ if (Object.prototype.hasOwnProperty.call(meta, key)) {
49
+ const data = meta[key]
50
+
51
+ for (let i = 0; i < data.length; i++) {
52
+ const item = data[i]
53
+ let suffix = ''
54
+
55
+ if (i > 0) {
56
+ suffix = '_' + i
57
+ }
58
+
59
+ pageValues[item.name + suffix] = item.content
60
+ }
61
+ }
62
+ }
63
+
64
+ const component = await createComponent({
65
+ id: options.componentId,
66
+ values: pageValues,
67
+ components,
68
+ document
69
+ })
70
+
71
+ result = result.concat(component.children)
72
+ }
73
+
74
+ return result
75
+ }
package/lib/index.js ADDED
@@ -0,0 +1,8 @@
1
+ import getHTML from './get-html.js'
2
+
3
+ export * from './parse.js'
4
+ export * from './path-utils.js'
5
+
6
+ export {
7
+ getHTML
8
+ }