@podlite/schema 0.0.36 → 0.0.38

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.
@@ -4,6 +4,7 @@ export declare type Context = {
4
4
  export interface Attr {
5
5
  getAllValues: (name: any) => any;
6
6
  getFirstValue: (name: any) => any;
7
+ getMapValue: (name: string) => Record<string, string | number | boolean> | undefined;
7
8
  asHash: () => {};
8
9
  (): {};
9
10
  exists(name: string): boolean;
@@ -43,6 +43,14 @@ const makeAttrs = (node, ctx = {}) => {
43
43
  resfn.getFirstValue = name => {
44
44
  return resfn.exists(name) ? resfn.getAllValues(name)[0] : undefined;
45
45
  };
46
+ /**
47
+ * return first map-typed value (from `:attr{ k=>v, ... }` syntax) or
48
+ * undefined when the attribute is absent or not a map value
49
+ */
50
+ resfn.getMapValue = name => {
51
+ const first = resfn.getFirstValue(name);
52
+ return first && typeof first === 'object' && !Array.isArray(first) ? first : undefined;
53
+ };
46
54
  /**
47
55
  * return key: val
48
56
  *
@@ -1,5 +1,2 @@
1
1
  declare const _default: () => (tree: any) => any;
2
- /**
3
- * Main transforms
4
- */
5
2
  export default _default;
@@ -82,9 +82,35 @@ const extractColumnsByTemplate = (text, template) => {
82
82
  /**
83
83
  * Main transforms
84
84
  */
85
+ const wrapImplicitCells = rowNode => {
86
+ if (!Array.isArray(rowNode.content) || rowNode.content.length === 0)
87
+ return rowNode;
88
+ const hasNonCell = rowNode.content.some(c => c && c.type === 'block' && c.name !== 'cell' && c.name !== 'blankline');
89
+ if (!hasNonCell)
90
+ return rowNode;
91
+ const wrapped = rowNode.content.map(c => {
92
+ if (!c || c.type !== 'block')
93
+ return c;
94
+ if (c.name === 'cell' || c.name === 'blankline')
95
+ return c;
96
+ return { type: 'block', name: 'cell', content: [c], margin: c.margin || '' };
97
+ });
98
+ return { ...rowNode, content: wrapped };
99
+ };
100
+ const isStructured = tableNode => Array.isArray(tableNode.content) &&
101
+ tableNode.content.some(c => c && c.type === 'block' && (c.name === 'row' || c.name === 'cell'));
85
102
  exports.default = () => tree => {
86
103
  const transformer = (0, makeTransformer_1.default)({
87
104
  table: node => {
105
+ // structured mode: transform row children (wrap implicit cells)
106
+ if (isStructured(node)) {
107
+ const transformedContent = (node.content || []).map(c => {
108
+ if (c && c.name === 'row')
109
+ return wrapImplicitCells(c);
110
+ return c;
111
+ });
112
+ return { ...node, content: transformedContent };
113
+ }
88
114
  let rows = [];
89
115
  const collectValues = row => {
90
116
  rows.push(row.value);
@@ -116,9 +142,12 @@ exports.default = () => tree => {
116
142
  },
117
143
  })(node);
118
144
  const columnTemplate = makeMask(lines, separators);
119
- const makeBlock = (name, content, ...attr) => {
120
- return { ...attr, name, type: 'block', content: Array.isArray(content) ? content : [content] };
145
+ const makeBlock = (name, content, extra = {}) => {
146
+ return { ...extra, name, type: 'block', content: Array.isArray(content) ? content : [content] };
121
147
  };
148
+ const makeRow = cells => makeBlock('row', cells);
149
+ const makeHeaderRow = cells => makeBlock('row', cells, { config: [{ name: 'header', value: true, type: 'boolean' }] });
150
+ const makeCell = text => makeBlock('cell', { type: 'text', value: text });
122
151
  // make columns
123
152
  const res = (0, makeTransformer_1.default)({
124
153
  'row:text': row => {
@@ -126,16 +155,16 @@ exports.default = () => tree => {
126
155
  // split each text row into lines
127
156
  const textRowsLines = flattenDeep([row.value].map(splitToLines));
128
157
  return textRowsLines.map(rowValue => {
129
- const res = extractColumnsByTemplate(rowValue, columnTemplate);
130
- return makeBlock('table_row', res.map(col => makeBlock('table_cell', { type: 'text', value: col })));
158
+ const cols = extractColumnsByTemplate(rowValue, columnTemplate);
159
+ return makeRow(cols.map(makeCell));
131
160
  });
132
161
  }
133
- const res = extractColumnsByTemplate(row.value, columnTemplate);
134
- return makeBlock('table_row', res.map(col => makeBlock('table_cell', { type: 'text', value: col })));
162
+ const cols = extractColumnsByTemplate(row.value, columnTemplate);
163
+ return makeRow(cols.map(makeCell));
135
164
  },
136
165
  'head:text': head => {
137
- const res = extractColumnsByTemplate(head.value, columnTemplate);
138
- return makeBlock('table_head', res.map(col => makeBlock('table_cell', { type: 'text', value: col })));
166
+ const cols = extractColumnsByTemplate(head.value, columnTemplate);
167
+ return makeHeaderRow(cols.map(makeCell));
139
168
  },
140
169
  })(node);
141
170
  return res;
package/lib/types.d.ts CHANGED
@@ -24,7 +24,9 @@ export interface RulesStrict {
24
24
  'D<>': RuleHandler<FormattingCodeD>;
25
25
  'E<>': RuleHandler<FormattingCodeE>;
26
26
  'F<>': RuleHandler<FormattingCodeF>;
27
+ 'H<>': RuleHandler<FormattingCodeAny>;
27
28
  'I<>': RuleHandler<FormattingCodeI>;
29
+ 'J<>': RuleHandler<FormattingCodeAny>;
28
30
  'K<>': RuleHandler<FormattingCodeAny>;
29
31
  'R<>': RuleHandler<FormattingCodeAny>;
30
32
  'T<>': RuleHandler<FormattingCodeAny>;
@@ -35,7 +37,7 @@ export interface RulesStrict {
35
37
  'L<>': RuleHandler<FormattingCodeL>;
36
38
  'U<>': RuleHandler<FormattingCodeAny>;
37
39
  'Z<>': RuleHandler<FormattingCodeAny>;
38
- 'Delete<>': RuleHandler<FormattingCodeAny>;
40
+ 'O<>': RuleHandler<FormattingCodeAny>;
39
41
  pod: RuleHandler<Para>;
40
42
  root: RuleHandler<RootBlock>;
41
43
  ':para': RuleHandler<Para>;
@@ -68,9 +70,8 @@ export interface RulesStrict {
68
70
  'table:block': RuleHandler<BlockTable>;
69
71
  table: RuleHandler<BlockTable>;
70
72
  ':separator': RuleHandler<Separator>;
71
- table_row: RuleHandler<TableRow>;
72
- table_cell: RuleHandler<TableCell>;
73
- table_head: RuleHandler<TableHead>;
73
+ row: RuleHandler<TableRow>;
74
+ cell: RuleHandler<TableCell>;
74
75
  toc: RuleHandler<BlockToc>;
75
76
  ':toc': RuleHandler<Toc>;
76
77
  ':toc-list': RuleHandler<TocList>;
@@ -100,6 +101,7 @@ export interface Image {
100
101
  export interface Toc {
101
102
  type: 'toc';
102
103
  title?: string;
104
+ folded?: boolean;
103
105
  foldedLevels?: Record<number, boolean>;
104
106
  content: TocList;
105
107
  }
@@ -342,17 +344,17 @@ export interface BlockTable extends Omit<Block, 'content'> {
342
344
  text?: string;
343
345
  }
344
346
  export interface TableCell {
345
- name: 'table_cell';
347
+ name: 'cell';
346
348
  type: 'block';
347
349
  content: Array<string>;
348
350
  }
349
351
  export interface TableRow {
350
- name: 'table_row';
352
+ name: 'row';
351
353
  type: 'block';
352
354
  content: Array<TableCell>;
353
355
  }
354
356
  export interface TableHead {
355
- name: 'table_head';
357
+ name: 'row';
356
358
  type: 'block';
357
359
  content: Array<TableCell>;
358
360
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@podlite/schema",
3
- "version": "0.0.36",
3
+ "version": "0.0.38",
4
4
  "description": "AST tools and schema for Podlite markup language — validate, traverse, and transform document trees",
5
5
  "main": "lib/index.js",
6
6
  "homepage": "https://podlite.org",
@@ -68,7 +68,7 @@
68
68
  "@types/node": "^17.0.7",
69
69
  "glob": "^7.2.0",
70
70
  "npm-run-all": "^4.1.5",
71
- "pegmill": "0.1.0",
71
+ "pegmill": "0.1.2",
72
72
  "ts-node": "^9.1.1",
73
73
  "typescript": "4.5.5",
74
74
  "typescript-json-schema": "0.53.0"
@@ -1521,7 +1521,7 @@
1521
1521
  "name": {
1522
1522
  "type": "string",
1523
1523
  "enum": [
1524
- "table_row"
1524
+ "row"
1525
1525
  ]
1526
1526
  },
1527
1527
  "type": {
@@ -1549,7 +1549,7 @@
1549
1549
  "name": {
1550
1550
  "type": "string",
1551
1551
  "enum": [
1552
- "table_cell"
1552
+ "cell"
1553
1553
  ]
1554
1554
  },
1555
1555
  "type": {
@@ -1577,7 +1577,7 @@
1577
1577
  "name": {
1578
1578
  "type": "string",
1579
1579
  "enum": [
1580
- "table_head"
1580
+ "row"
1581
1581
  ]
1582
1582
  },
1583
1583
  "type": {
@@ -2688,6 +2688,9 @@
2688
2688
  "title": {
2689
2689
  "type": "string"
2690
2690
  },
2691
+ "folded": {
2692
+ "type": "boolean"
2693
+ },
2691
2694
  "foldedLevels": {
2692
2695
  "type": "object",
2693
2696
  "additionalProperties": false,
@@ -1521,7 +1521,7 @@
1521
1521
  "name": {
1522
1522
  "type": "string",
1523
1523
  "enum": [
1524
- "table_row"
1524
+ "row"
1525
1525
  ]
1526
1526
  },
1527
1527
  "type": {
@@ -1549,7 +1549,7 @@
1549
1549
  "name": {
1550
1550
  "type": "string",
1551
1551
  "enum": [
1552
- "table_cell"
1552
+ "cell"
1553
1553
  ]
1554
1554
  },
1555
1555
  "type": {
@@ -1577,7 +1577,7 @@
1577
1577
  "name": {
1578
1578
  "type": "string",
1579
1579
  "enum": [
1580
- "table_head"
1580
+ "row"
1581
1581
  ]
1582
1582
  },
1583
1583
  "type": {
@@ -2688,6 +2688,9 @@
2688
2688
  "title": {
2689
2689
  "type": "string"
2690
2690
  },
2691
+ "folded": {
2692
+ "type": "boolean"
2693
+ },
2691
2694
  "foldedLevels": {
2692
2695
  "type": "object",
2693
2696
  "additionalProperties": false,