@markdoc/markdoc 0.5.1 → 0.5.3
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/dist/index.js +27 -6
- package/dist/index.js.map +3 -3
- package/dist/index.mjs +27 -6
- package/dist/index.mjs.map +3 -3
- package/dist/src/formatter.d.ts +1 -1
- package/dist/src/formatter.d.ts.map +1 -1
- package/dist/src/schema-types/conditional.d.ts +13 -0
- package/dist/src/schema-types/conditional.d.ts.map +1 -0
- package/dist/src/tags/conditional.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/formatter.test.ts +5 -0
- package/src/formatter.ts +12 -4
- package/src/schema-types/conditional.ts +34 -0
- package/src/tags/conditional.ts +3 -2
- package/src/tokenizer/plugins/comments.test.ts +24 -0
- package/src/tokenizer/plugins/comments.ts +2 -2
package/dist/src/formatter.d.ts
CHANGED
|
@@ -6,6 +6,6 @@ declare type Options = {
|
|
|
6
6
|
parent?: Node;
|
|
7
7
|
indent?: number;
|
|
8
8
|
};
|
|
9
|
-
export default function format(v: Value, options?: Options): string;
|
|
9
|
+
export default function format(v: Value | Value[], options?: Options): string;
|
|
10
10
|
export {};
|
|
11
11
|
//# sourceMappingURL=formatter.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"formatter.d.ts","sourceRoot":"","sources":["../../src/formatter.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAA4B,IAAI,EAAE,KAAK,EAAY,MAAM,SAAS,CAAC;AAE/E,aAAK,OAAO,GAAG;IACb,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,eAAe,CAAC,EAAE,WAAW,GAAG,QAAQ,CAAC;IACzC,MAAM,CAAC,EAAE,IAAI,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;
|
|
1
|
+
{"version":3,"file":"formatter.d.ts","sourceRoot":"","sources":["../../src/formatter.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAA4B,IAAI,EAAE,KAAK,EAAY,MAAM,SAAS,CAAC;AAE/E,aAAK,OAAO,GAAG;IACb,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,eAAe,CAAC,EAAE,WAAW,GAAG,QAAQ,CAAC;IACzC,MAAM,CAAC,EAAE,IAAI,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AA2eF,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,CAAC,EAAE,KAAK,GAAG,KAAK,EAAE,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,MAAM,CAI5E"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Config, CustomAttributeTypeInterface, ValidationError } from '../types';
|
|
2
|
+
/**
|
|
3
|
+
* Conditional attribute type for Markdoc {% if /%} and {% else /%} tags.
|
|
4
|
+
*
|
|
5
|
+
* Acceptable values are:
|
|
6
|
+
* - `boolean` (true or false)
|
|
7
|
+
* - `null` or `undefined` (in case of using variable that's not defined)
|
|
8
|
+
* - `object`
|
|
9
|
+
*/
|
|
10
|
+
export declare class ConditionalAttributeType implements CustomAttributeTypeInterface {
|
|
11
|
+
validate(value: any, _config: Config, key: string): ValidationError[];
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=conditional.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conditional.d.ts","sourceRoot":"","sources":["../../../src/schema-types/conditional.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EACN,4BAA4B,EAC5B,eAAe,EAChB,MAAM,UAAU,CAAC;AAElB;;;;;;;GAOG;AACH,qBAAa,wBAAyB,YAAW,4BAA4B;IAC3E,QAAQ,CAAC,KAAK,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,eAAe,EAAE;CAkBtE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conditional.d.ts","sourceRoot":"","sources":["../../../src/tags/conditional.ts"],"names":[],"mappings":"AAEA,OAAO,EAKL,MAAM,EAEP,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"conditional.d.ts","sourceRoot":"","sources":["../../../src/tags/conditional.ts"],"names":[],"mappings":"AAEA,OAAO,EAKL,MAAM,EAEP,MAAM,UAAU,CAAC;AAKlB,wBAAgB,MAAM,CAAC,KAAK,EAAE,GAAG,WAEhC;AAmBD,eAAO,MAAM,KAAK,EAAE,MAmBnB,CAAC;AAEF,eAAO,MAAM,OAAO,EAAE,MAKrB,CAAC"}
|
package/package.json
CHANGED
package/src/formatter.test.ts
CHANGED
|
@@ -149,17 +149,22 @@ regular_word_with_underscores
|
|
|
149
149
|
|
|
150
150
|
\\### Heading
|
|
151
151
|
|
|
152
|
+
#Not a heading
|
|
153
|
+
|
|
152
154
|
**/docs/\\***
|
|
153
155
|
|
|
154
156
|
~~**a \\_sentence\\_ with \\_underscores**~~
|
|
155
157
|
|
|
156
158
|
- Item with [brackets]
|
|
157
159
|
|
|
160
|
+
-not a list item
|
|
161
|
+
|
|
158
162
|
\`\`\`
|
|
159
163
|
\\*\\_[\\[]
|
|
160
164
|
\`\`\`
|
|
161
165
|
|
|
162
166
|
{% table %}
|
|
167
|
+
- <https://autolink.com>
|
|
163
168
|
- **[Link](https://example.com?q=()**
|
|
164
169
|
- **[Link](https://example.com?q=\\()**
|
|
165
170
|
- **[Link](https://example.com?q=\\(\\))**
|
package/src/formatter.ts
CHANGED
|
@@ -190,8 +190,16 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
190
190
|
break;
|
|
191
191
|
}
|
|
192
192
|
case 'link': {
|
|
193
|
+
const children = [...formatChildren(n, no)].join('');
|
|
194
|
+
|
|
195
|
+
// https://spec.commonmark.org/0.31.2/#autolinks
|
|
196
|
+
if (children === n.attributes.href && !n.attributes.title) {
|
|
197
|
+
yield `<${n.attributes.href}>`;
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
|
|
193
201
|
yield '[';
|
|
194
|
-
yield
|
|
202
|
+
yield children;
|
|
195
203
|
yield ']';
|
|
196
204
|
yield '(';
|
|
197
205
|
yield* typeof n.attributes.href === 'string'
|
|
@@ -216,7 +224,7 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
216
224
|
yield* escapeMarkdownCharacters(content, /[*_~]/g);
|
|
217
225
|
} else {
|
|
218
226
|
// Escape > blockquote, * list item, and heading
|
|
219
|
-
yield* escapeMarkdownCharacters(content,
|
|
227
|
+
yield* escapeMarkdownCharacters(content, /^\*|#+\s|>/);
|
|
220
228
|
}
|
|
221
229
|
}
|
|
222
230
|
|
|
@@ -453,7 +461,7 @@ function* formatNode(n: Node, o: Options = {}) {
|
|
|
453
461
|
}
|
|
454
462
|
|
|
455
463
|
function* formatValue(
|
|
456
|
-
v: Value,
|
|
464
|
+
v: Value | Value[],
|
|
457
465
|
o: Options = {}
|
|
458
466
|
): Generator<string, void, unknown> {
|
|
459
467
|
switch (typeof v) {
|
|
@@ -491,7 +499,7 @@ function* formatValue(
|
|
|
491
499
|
}
|
|
492
500
|
}
|
|
493
501
|
|
|
494
|
-
export default function format(v: Value, options?: Options): string {
|
|
502
|
+
export default function format(v: Value | Value[], options?: Options): string {
|
|
495
503
|
let doc = '';
|
|
496
504
|
for (const s of formatValue(v, options)) doc += s;
|
|
497
505
|
return doc.trimStart();
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Config,
|
|
3
|
+
CustomAttributeTypeInterface,
|
|
4
|
+
ValidationError,
|
|
5
|
+
} from '../types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Conditional attribute type for Markdoc {% if /%} and {% else /%} tags.
|
|
9
|
+
*
|
|
10
|
+
* Acceptable values are:
|
|
11
|
+
* - `boolean` (true or false)
|
|
12
|
+
* - `null` or `undefined` (in case of using variable that's not defined)
|
|
13
|
+
* - `object`
|
|
14
|
+
*/
|
|
15
|
+
export class ConditionalAttributeType implements CustomAttributeTypeInterface {
|
|
16
|
+
validate(value: any, _config: Config, key: string): ValidationError[] {
|
|
17
|
+
if (
|
|
18
|
+
typeof value === 'boolean' ||
|
|
19
|
+
value === null ||
|
|
20
|
+
value === undefined ||
|
|
21
|
+
typeof value === 'object'
|
|
22
|
+
) {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return [
|
|
27
|
+
{
|
|
28
|
+
id: 'attribute-type-invalid',
|
|
29
|
+
level: 'error',
|
|
30
|
+
message: `Attribute '${key}' must be type 'boolean | object' (null or undefined are also allowed)`,
|
|
31
|
+
},
|
|
32
|
+
];
|
|
33
|
+
}
|
|
34
|
+
}
|
package/src/tags/conditional.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
Schema,
|
|
9
9
|
Value,
|
|
10
10
|
} from '../types';
|
|
11
|
+
import { ConditionalAttributeType } from '../schema-types/conditional';
|
|
11
12
|
|
|
12
13
|
type Condition = { condition: Value; children: Node[] };
|
|
13
14
|
|
|
@@ -34,7 +35,7 @@ function renderConditions(node: Node) {
|
|
|
34
35
|
|
|
35
36
|
export const tagIf: Schema = {
|
|
36
37
|
attributes: {
|
|
37
|
-
primary: { type:
|
|
38
|
+
primary: { type: ConditionalAttributeType, render: false },
|
|
38
39
|
},
|
|
39
40
|
|
|
40
41
|
transform(node, config) {
|
|
@@ -56,6 +57,6 @@ export const tagIf: Schema = {
|
|
|
56
57
|
export const tagElse: Schema = {
|
|
57
58
|
selfClosing: true,
|
|
58
59
|
attributes: {
|
|
59
|
-
primary: { type:
|
|
60
|
+
primary: { type: ConditionalAttributeType, render: false },
|
|
60
61
|
},
|
|
61
62
|
};
|
|
@@ -90,5 +90,29 @@ describe('MarkdownIt Comments plugin', function () {
|
|
|
90
90
|
|
|
91
91
|
expect(example).toDeepEqualSubset(output);
|
|
92
92
|
});
|
|
93
|
+
|
|
94
|
+
it('block comment across multiple lines with blank lines', function () {
|
|
95
|
+
const actual = tokenizer.tokenize(
|
|
96
|
+
`
|
|
97
|
+
foo <!-- example
|
|
98
|
+
|
|
99
|
+
comment --> bar
|
|
100
|
+
`.trim()
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
const expected = [
|
|
104
|
+
{ type: 'paragraph_open' },
|
|
105
|
+
{
|
|
106
|
+
type: 'inline',
|
|
107
|
+
children: [{ type: 'text', content: 'foo <!-- example' }],
|
|
108
|
+
},
|
|
109
|
+
{ type: 'paragraph_close' },
|
|
110
|
+
{ type: 'paragraph_open' },
|
|
111
|
+
{ type: 'inline', content: 'comment --> bar' },
|
|
112
|
+
{ type: 'paragraph_close' },
|
|
113
|
+
];
|
|
114
|
+
|
|
115
|
+
expect(actual).toDeepEqualSubset(expected);
|
|
116
|
+
});
|
|
93
117
|
});
|
|
94
118
|
});
|
|
@@ -16,7 +16,7 @@ function block(
|
|
|
16
16
|
|
|
17
17
|
const close = state.src.indexOf(CLOSE, start);
|
|
18
18
|
|
|
19
|
-
if (
|
|
19
|
+
if (close === -1) return false;
|
|
20
20
|
if (silent) return true;
|
|
21
21
|
|
|
22
22
|
const content = state.src.slice(start + OPEN.length, close);
|
|
@@ -34,7 +34,7 @@ function inline(state: StateInline, silent: boolean): boolean {
|
|
|
34
34
|
|
|
35
35
|
const close = state.src.indexOf(CLOSE, state.pos);
|
|
36
36
|
|
|
37
|
-
if (
|
|
37
|
+
if (close === -1) return false;
|
|
38
38
|
if (silent) return true;
|
|
39
39
|
|
|
40
40
|
const content = state.src.slice(state.pos + OPEN.length, close);
|