@adobe/helix-md2jcr 1.3.0 → 1.4.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adobe/helix-md2jcr",
3
- "version": "1.3.0",
3
+ "version": "1.4.1",
4
4
  "description": "Helix Service that converts markdown to JCR content structure",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -0,0 +1,112 @@
1
+ /*
2
+ * Copyright 2026 Adobe. All rights reserved.
3
+ * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
+ * you may not use this file except in compliance with the License. You may obtain a copy
5
+ * of the License at http://www.apache.org/licenses/LICENSE-2.0
6
+ *
7
+ * Unless required by applicable law or agreed to in writing, software distributed under
8
+ * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9
+ * OF ANY KIND, either express or implied. See the License for the specific language
10
+ * governing permissions and limitations under the License.
11
+ */
12
+ import { isGroupField } from './Models.js';
13
+
14
+ /**
15
+ * @typedef {import('../index.d.ts').ModelDef} Model
16
+ */
17
+
18
+ /**
19
+ * Recursively collect every option `value` from a field's options tree
20
+ * (options may be nested under `children`).
21
+ * @param {Array<object>} options - The field's options array.
22
+ * @return {Array<string>} The flat list of option values.
23
+ */
24
+ function collectOptionValues(options) {
25
+ return (options || []).flatMap((option) => {
26
+ if (option.children) {
27
+ return collectOptionValues(option.children);
28
+ }
29
+ return option.value !== undefined ? [option.value] : [];
30
+ });
31
+ }
32
+
33
+ /**
34
+ * Distribute element-grouping tokens (a block header's classes, a container
35
+ * child's leading cell, or a section's `style` cell) across the model's group
36
+ * fields, per the AEM "element grouping for block options" rules:
37
+ *
38
+ * - a boolean field receives "true" when its name suffix (after `${group}_`)
39
+ * appears as a token (e.g. token "fullwidth" -> `${group}_fullwidth`="true");
40
+ * - a text/select field with options claims the tokens that match its option
41
+ * values (e.g. token "light" -> `${group}_background`="light");
42
+ * - any remaining tokens fall back to the base `${group}` field, which also
43
+ * covers the common single-field case (a free-form `${group}` field).
44
+ *
45
+ * Multi fields are written as `[a, b]`; single fields as a comma-separated list.
46
+ * The result is emitted in model-field order so callers that do not sort
47
+ * (e.g. sections) get deterministic, readable output.
48
+ * @param {Model} model - The block, item, or section model.
49
+ * @param {Array<string>} tokens - The option tokens.
50
+ * @param {string} [group] - The group name (defaults to `classes`).
51
+ * @return {Object<string, string>} A map of group field name to property value.
52
+ */
53
+ function distributeGroupOptions(model, tokens, group = 'classes') {
54
+ const groupFields = (model?.fields || []).filter((f) => isGroupField(f.name, group));
55
+ if (groupFields.length === 0 || tokens.length === 0) {
56
+ return {};
57
+ }
58
+
59
+ const suffixRegExp = new RegExp(`^${group}_`);
60
+ const remaining = [...tokens];
61
+ const assigned = new Map();
62
+
63
+ // boolean fields: the suffix after `${group}_` becomes the option when present
64
+ groupFields
65
+ .filter((field) => field.component === 'boolean')
66
+ .forEach((field) => {
67
+ const suffix = field.name.replace(suffixRegExp, '');
68
+ const idx = remaining.indexOf(suffix);
69
+ if (idx !== -1) {
70
+ assigned.set(field.name, true);
71
+ remaining.splice(idx, 1);
72
+ }
73
+ });
74
+
75
+ // text/select fields with options: claim the tokens that match their values
76
+ groupFields
77
+ .filter((field) => field.component !== 'boolean' && field.options)
78
+ .forEach((field) => {
79
+ const values = collectOptionValues(field.options);
80
+ const matched = remaining.filter((t) => values.includes(t));
81
+ if (matched.length > 0) {
82
+ assigned.set(field.name, matched);
83
+ matched.forEach((m) => remaining.splice(remaining.indexOf(m), 1));
84
+ }
85
+ });
86
+
87
+ // leftover tokens fall back to the base `${group}` field (and the single-field case)
88
+ const base = groupFields.find((f) => f.name === group && f.component !== 'boolean');
89
+ if (remaining.length > 0 && base) {
90
+ assigned.set(group, [...(assigned.get(group) || []), ...remaining]);
91
+ }
92
+
93
+ // emit in model-field order for deterministic, readable output
94
+ const result = {};
95
+ groupFields.forEach((field) => {
96
+ if (!assigned.has(field.name)) {
97
+ return;
98
+ }
99
+ const value = assigned.get(field.name);
100
+ if (value === true) {
101
+ result[field.name] = 'true';
102
+ } else {
103
+ result[field.name] = field.multi === true ? `[${value.join(', ')}]` : value.join(', ');
104
+ }
105
+ });
106
+ return result;
107
+ }
108
+
109
+ export {
110
+ collectOptionValues,
111
+ distributeGroupOptions,
112
+ };
@@ -41,6 +41,18 @@ function getField(model, fieldName) {
41
41
  return model ? model.fields.find((field) => field.name === fieldName) : undefined;
42
42
  }
43
43
 
44
+ /**
45
+ * Returns true if the field name belongs to an element-grouping group — either
46
+ * the base field named exactly `group`, or a grouped option field `group_<name>`
47
+ * (element grouping for block options / section styles).
48
+ * @param {string} name The field name.
49
+ * @param {string} group The group name (e.g. `classes` or `style`).
50
+ * @return {boolean}
51
+ */
52
+ function isGroupField(name, group) {
53
+ return name === group || name.startsWith(`${group}_`);
54
+ }
55
+
44
56
  /**
45
57
  * Returns true if the field name belongs to the `classes` block-options group —
46
58
  * either the base `classes` field or a grouped `classes_*` option field
@@ -49,12 +61,14 @@ function getField(model, fieldName) {
49
61
  * @return {boolean}
50
62
  */
51
63
  function isClassesField(name) {
52
- return name === 'classes' || name.startsWith('classes_');
64
+ return isGroupField(name, 'classes');
53
65
  }
54
66
 
55
67
  /**
56
- * Return a list of field names in the model, excluding the `classes` group
57
- * (those fields are block options, not content).
68
+ * Return a list of field names in the model. Block-option / section-style group
69
+ * fields (`classes`, `classes_*`, `style`, `style_*`) are included so the
70
+ * Universal Editor can render their controls; only `tab` fields (a UE layout
71
+ * construct, not a content field) are excluded.
58
72
  * @param {Model} model The model.
59
73
  * @return {Array<string>} An array of field names.
60
74
  */
@@ -63,7 +77,6 @@ function getModelFieldNames(model) {
63
77
  ? model.fields
64
78
  .filter((f) => f.component !== 'tab')
65
79
  .map((f) => f.name)
66
- .filter((f) => !isClassesField(f))
67
80
  : [];
68
81
  }
69
82
 
@@ -72,4 +85,5 @@ export {
72
85
  findModelById,
73
86
  getModelFieldNames,
74
87
  isClassesField,
88
+ isGroupField,
75
89
  };
@@ -18,6 +18,7 @@ import {
18
18
  getField,
19
19
  getModelFieldNames,
20
20
  } from '../../domain/Models.js';
21
+ import { distributeGroupOptions } from '../../domain/GroupOptions.js';
21
22
 
22
23
  /**
23
24
  * Normalizes strings by converting to lowercase and replacing spaces with hyphens
@@ -61,6 +62,12 @@ function processMetadataRows(rows, model) {
61
62
  // always push the model fields so UE can use them, even if no attributes are found
62
63
  const modelFields = getModelFieldNames(model);
63
64
 
65
+ // a section uses element grouping when it declares a `style_*` field; in that
66
+ // case the single well-known `style` cell collapses the whole style group and
67
+ // must be distributed back across style / style_* fields. A model with only a
68
+ // base `style` field keeps the simple per-row mapping below.
69
+ const hasStyleGroup = (model?.fields || []).some((f) => f.name.startsWith('style_'));
70
+
64
71
  for (const row of rows) {
65
72
  const cells = findAll(row, (n) => n.type === 'gtCell', true);
66
73
  if (cells.length < 2) {
@@ -77,6 +84,22 @@ function processMetadataRows(rows, model) {
77
84
  continue;
78
85
  }
79
86
 
87
+ // element grouping for section styles: route the flat `style` cell across
88
+ // the model's style group fields (boolean by name suffix, select/multiselect
89
+ // by declared options, leftovers to the base `style` field)
90
+ if (hasStyleGroup && key === 'style') {
91
+ Object.assign(
92
+ attributes,
93
+ distributeGroupOptions(
94
+ model,
95
+ value.split(',').map((s) => s.trim()).filter(Boolean),
96
+ 'style',
97
+ ),
98
+ );
99
+ // eslint-disable-next-line no-continue
100
+ continue;
101
+ }
102
+
80
103
  const field = getField(model, key);
81
104
  if (!field) {
82
105
  // eslint-disable-next-line no-continue
@@ -21,6 +21,7 @@ import {
21
21
  findModelById,
22
22
  getModelFieldNames, isClassesField,
23
23
  } from '../../domain/Models.js';
24
+ import { distributeGroupOptions } from '../../domain/GroupOptions.js';
24
25
  import { findAll } from '../../utils/mdast.js';
25
26
  import link from './supports/link.js';
26
27
  import {
@@ -38,91 +39,6 @@ import ModelHelper from '../../domain/ModelHelper.js';
38
39
  * @typedef {import('../../index.d.ts').Filter} Filters
39
40
  */
40
41
 
41
- // -- Block Options (classes) --------------------------------------------------
42
-
43
- /**
44
- * Recursively collect every option `value` from a field's options tree
45
- * (options may be nested under `children`).
46
- * @param {Array<object>} options - The field's options array.
47
- * @return {Array<string>} The flat list of option values.
48
- */
49
- function collectOptionValues(options) {
50
- return (options || []).flatMap((option) => {
51
- if (option.children) {
52
- return collectOptionValues(option.children);
53
- }
54
- return option.value !== undefined ? [option.value] : [];
55
- });
56
- }
57
-
58
- /**
59
- * Distribute block-option tokens (the classes from a block header, or the
60
- * leading cell of a child item) across the model's `classes` group fields, per
61
- * the AEM "element grouping for block options" rules:
62
- *
63
- * - a boolean field receives "true" when its name suffix (after `classes_`)
64
- * appears as a token (e.g. token "fullwidth" -> classes_fullwidth="true");
65
- * - a text/select field with options claims the tokens that match its option
66
- * values (e.g. token "light" -> classes_background="light");
67
- * - any remaining tokens fall back to the base `classes` field, which also
68
- * covers the common single-field case (a free-form `classes` field).
69
- *
70
- * Multi fields are written as `[a, b]`; single fields as a comma-separated list.
71
- * @param {Model} model - The block or item model.
72
- * @param {Array<string>} tokens - The option tokens.
73
- * @return {Object<string, string>} A map of class field name to property value.
74
- */
75
- function distributeBlockOptions(model, tokens) {
76
- const groupFields = (model?.fields || []).filter((f) => isClassesField(f.name));
77
- if (groupFields.length === 0 || tokens.length === 0) {
78
- return {};
79
- }
80
-
81
- const remaining = [...tokens];
82
- const assigned = new Map();
83
-
84
- // boolean fields: the suffix after `classes_` becomes the option when present
85
- groupFields
86
- .filter((field) => field.component === 'boolean')
87
- .forEach((field) => {
88
- const suffix = field.name.replace(/^classes_/, '');
89
- const idx = remaining.indexOf(suffix);
90
- if (idx !== -1) {
91
- assigned.set(field.name, true);
92
- remaining.splice(idx, 1);
93
- }
94
- });
95
-
96
- // text/select fields with options: claim the tokens that match their values
97
- groupFields
98
- .filter((field) => field.component !== 'boolean' && field.options)
99
- .forEach((field) => {
100
- const values = collectOptionValues(field.options);
101
- const matched = remaining.filter((t) => values.includes(t));
102
- if (matched.length > 0) {
103
- assigned.set(field.name, matched);
104
- matched.forEach((m) => remaining.splice(remaining.indexOf(m), 1));
105
- }
106
- });
107
-
108
- // leftover tokens fall back to the base `classes` field (and the single-field case)
109
- const base = groupFields.find((f) => f.name === 'classes' && f.component !== 'boolean');
110
- if (remaining.length > 0 && base) {
111
- assigned.set('classes', [...(assigned.get('classes') || []), ...remaining]);
112
- }
113
-
114
- const result = {};
115
- assigned.forEach((value, name) => {
116
- if (value === true) {
117
- result[name] = 'true';
118
- } else {
119
- const field = groupFields.find((f) => f.name === name);
120
- result[name] = field.multi === true ? `[${value.join(', ')}]` : value.join(', ');
121
- }
122
- });
123
- return result;
124
- }
125
-
126
42
  // -- Block Header -------------------------------------------------------------
127
43
 
128
44
  /**
@@ -193,7 +109,7 @@ function extractBlockHeaderProperties(models, definition, mdast) {
193
109
  // section metadata may not have a model. Distribute the header's block options
194
110
  // across the model's classes group fields (element grouping for block options).
195
111
  if (model) {
196
- Object.assign(props, distributeBlockOptions(model, blockDetails.classes));
112
+ Object.assign(props, distributeGroupOptions(model, blockDetails.classes, 'classes'));
197
113
  }
198
114
 
199
115
  return props;
@@ -446,7 +362,7 @@ function extractProperties(
446
362
  classes.shift();
447
363
 
448
364
  // distribute the options across the model's classes group fields
449
- Object.assign(properties, distributeBlockOptions(model, classes));
365
+ Object.assign(properties, distributeGroupOptions(model, classes, 'classes'));
450
366
  }
451
367
  } else {
452
368
  // get rid of the header row, no need for that