@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.
@@ -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;AA4XF,MAAM,CAAC,OAAO,UAAU,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,OAAO,GAAG,MAAM,CAIlE"}
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"}
@@ -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[];
@@ -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
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@markdoc/markdoc",
3
3
  "author": "Ryan Paul",
4
- "version": "0.1.9",
4
+ "version": "0.1.10",
5
5
  "description": "A text markup language for documentation",
6
6
  "main": "dist/index.js",
7
7
  "module": "dist/index.mjs",
@@ -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
- stable('');
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') return '#' + a.value;
61
- if (a.type === 'class') return '.' + a.name;
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
- if (key === 'class' && !Ast.isAst(value))
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.join('.');
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
  }