@markdoc/markdoc 0.1.9 → 0.1.10
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 +14 -4
- package/dist/index.js.map +2 -2
- package/dist/index.mjs +14 -4
- package/dist/index.mjs.map +2 -2
- package/dist/src/formatter.d.ts.map +1 -1
- package/dist/src/utils.d.ts +2 -0
- package/dist/src/utils.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/formatter.test.ts +16 -5
- package/src/formatter.ts +16 -5
- package/src/utils.ts +6 -0
|
@@ -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,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,MAAM,CAAC,EAAE,IAAI,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAuYF,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,MAAM,CAIlE"}
|
package/dist/src/utils.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type Token from 'markdown-it/lib/token';
|
|
2
2
|
export declare const OPEN = "{%";
|
|
3
3
|
export declare const CLOSE = "%}";
|
|
4
|
+
export declare const IDENTIFIER_REGEX: RegExp;
|
|
5
|
+
export declare function isIdentifier(s: string): boolean;
|
|
4
6
|
export declare function isPromise(a: any): a is Promise<any>;
|
|
5
7
|
export declare function findTagEnd(content: string, start?: number): number | null;
|
|
6
8
|
export declare function parseTags(content: string, firstLine?: number): Token[];
|
package/dist/src/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,KAAK,MAAM,uBAAuB,CAAC;AAQ/C,eAAO,MAAM,IAAI,OAAO,CAAC;AACzB,eAAO,MAAM,KAAK,OAAO,CAAC;AAE1B,wBAAgB,SAAS,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC,IAAI,OAAO,CAAC,GAAG,CAAC,CAEnD;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,SAAI,iBA0BpD;AAoBD,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,SAAI,GAAG,KAAK,EAAE,CA+DjE"}
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,KAAK,MAAM,uBAAuB,CAAC;AAQ/C,eAAO,MAAM,IAAI,OAAO,CAAC;AACzB,eAAO,MAAM,KAAK,OAAO,CAAC;AAE1B,eAAO,MAAM,gBAAgB,QAAqB,CAAC;AAEnD,wBAAgB,YAAY,CAAC,CAAC,EAAE,MAAM,GAAG,OAAO,CAE/C;AAED,wBAAgB,SAAS,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC,IAAI,OAAO,CAAC,GAAG,CAAC,CAEnD;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,SAAI,iBA0BpD;AAoBD,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,SAAS,SAAI,GAAG,KAAK,EAAE,CA+DjE"}
|
package/package.json
CHANGED
package/src/formatter.test.ts
CHANGED
|
@@ -84,13 +84,11 @@ function stable(source, options?) {
|
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
describe('Formatter', () => {
|
|
87
|
-
it('null', () => {
|
|
88
|
-
expect(format(null)).toBe('');
|
|
89
|
-
});
|
|
90
|
-
|
|
91
87
|
it('empty', () => {
|
|
88
|
+
expect(format(null)).toBe('');
|
|
92
89
|
check('', '');
|
|
93
|
-
|
|
90
|
+
check('\n\n\t\n \n \n\n', '');
|
|
91
|
+
stable('\n\n\t\n \n \n\n');
|
|
94
92
|
});
|
|
95
93
|
|
|
96
94
|
it('basics', () => {
|
|
@@ -109,6 +107,16 @@ subtitle: Subtitle
|
|
|
109
107
|
stable(source);
|
|
110
108
|
});
|
|
111
109
|
|
|
110
|
+
it('complex attributes', () => {
|
|
111
|
+
const source = `{% if $gates["<string_key>"].test["@var"] id="id with space" class="class with space" /%}`;
|
|
112
|
+
const expected = `{% if
|
|
113
|
+
$gates["<string_key>"].test["@var"]
|
|
114
|
+
id="id with space"
|
|
115
|
+
class="class with space" /%}
|
|
116
|
+
`;
|
|
117
|
+
check(source, expected);
|
|
118
|
+
});
|
|
119
|
+
|
|
112
120
|
it('attribute edge cases', () => {
|
|
113
121
|
const source = `{% key id=$user.name class=default($y, "test") %}Child{% /key %}`;
|
|
114
122
|
const expected = `
|
|
@@ -121,6 +129,7 @@ subtitle: Subtitle
|
|
|
121
129
|
|
|
122
130
|
it('variables', () => {
|
|
123
131
|
const source = `
|
|
132
|
+
{% tag "complex primary" /%}
|
|
124
133
|
{% if $primary %}
|
|
125
134
|
X
|
|
126
135
|
{% /if %}
|
|
@@ -128,6 +137,8 @@ X
|
|
|
128
137
|
{% key x=$user.name new=$flag /%}
|
|
129
138
|
`;
|
|
130
139
|
const expected = `
|
|
140
|
+
{% tag "complex primary" /%}
|
|
141
|
+
|
|
131
142
|
{% if $primary %}
|
|
132
143
|
X
|
|
133
144
|
{% /if %}
|
package/src/formatter.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Ast from './ast';
|
|
2
|
-
import { OPEN, CLOSE } from './utils';
|
|
2
|
+
import { OPEN, CLOSE, isIdentifier } from './utils';
|
|
3
3
|
import type { AttributeValue, Function, Node, Value, Variable } from './types';
|
|
4
4
|
|
|
5
5
|
type Options = {
|
|
@@ -57,14 +57,19 @@ function formatScalar(v: Value): string {
|
|
|
57
57
|
|
|
58
58
|
function formatAnnotationValue(a: AttributeValue): string {
|
|
59
59
|
if (a.name === 'primary') return formatScalar(a.value);
|
|
60
|
-
if (a.name === 'id' && typeof a.value === 'string'
|
|
61
|
-
|
|
60
|
+
if (a.name === 'id' && typeof a.value === 'string' && isIdentifier(a.value))
|
|
61
|
+
return '#' + a.value;
|
|
62
|
+
if (a.type === 'class' && isIdentifier(a.name)) return '.' + a.name;
|
|
62
63
|
return `${a.name}=${formatScalar(a.value)}`;
|
|
63
64
|
}
|
|
64
65
|
|
|
65
66
|
function* formatAttributes(n: Node) {
|
|
66
67
|
for (const [key, value] of Object.entries(n.attributes)) {
|
|
67
|
-
|
|
68
|
+
/**
|
|
69
|
+
* In cases where the class attribute is not a valid identifer, we treat it as a
|
|
70
|
+
* regular attribute without the '.' sigil
|
|
71
|
+
*/
|
|
72
|
+
if (key === 'class' && typeof value === 'object' && !Ast.isAst(value))
|
|
68
73
|
for (const name of Object.keys(value)) {
|
|
69
74
|
yield formatAnnotationValue({ type: 'class', name, value });
|
|
70
75
|
}
|
|
@@ -82,7 +87,13 @@ function* formatAnnotations(n: Node) {
|
|
|
82
87
|
|
|
83
88
|
function* formatVariable(v: Variable) {
|
|
84
89
|
yield '$';
|
|
85
|
-
yield v.path
|
|
90
|
+
yield v.path
|
|
91
|
+
.map((p, i) => {
|
|
92
|
+
if (i === 0) return p;
|
|
93
|
+
if (isIdentifier(String(p))) return '.' + p;
|
|
94
|
+
return `["${p}"]`;
|
|
95
|
+
})
|
|
96
|
+
.join('');
|
|
86
97
|
}
|
|
87
98
|
|
|
88
99
|
function* formatFunction(f: Function) {
|
package/src/utils.ts
CHANGED
|
@@ -13,6 +13,12 @@ enum STATES {
|
|
|
13
13
|
export const OPEN = '{%';
|
|
14
14
|
export const CLOSE = '%}';
|
|
15
15
|
|
|
16
|
+
export const IDENTIFIER_REGEX = /^[a-zA-Z0-9_-]+$/;
|
|
17
|
+
|
|
18
|
+
export function isIdentifier(s: string): boolean {
|
|
19
|
+
return IDENTIFIER_REGEX.test(s);
|
|
20
|
+
}
|
|
21
|
+
|
|
16
22
|
export function isPromise(a: any): a is Promise<any> {
|
|
17
23
|
return a && typeof a === 'object' && typeof a.then === 'function';
|
|
18
24
|
}
|