@markdoc/markdoc 0.1.8 → 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;AAyXF,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.8",
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",
@@ -32,7 +32,7 @@
32
32
  "engines": {
33
33
  "node": ">=14.7.0"
34
34
  },
35
- "dependencies": {
35
+ "peerDependencies": {
36
36
  "@types/markdown-it": "*",
37
37
  "@types/react": "*"
38
38
  },
@@ -84,8 +84,11 @@ function stable(source, options?) {
84
84
  }
85
85
 
86
86
  describe('Formatter', () => {
87
- it('null', () => {
87
+ it('empty', () => {
88
88
  expect(format(null)).toBe('');
89
+ check('', '');
90
+ check('\n\n\t\n \n \n\n', '');
91
+ stable('\n\n\t\n \n \n\n');
89
92
  });
90
93
 
91
94
  it('basics', () => {
@@ -93,6 +96,27 @@ describe('Formatter', () => {
93
96
  stable(expected);
94
97
  });
95
98
 
99
+ it('frontmatter', () => {
100
+ const source = `---
101
+ title: Title
102
+ subtitle: Subtitle
103
+ ---
104
+
105
+ `;
106
+ check(source, source);
107
+ stable(source);
108
+ });
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
+
96
120
  it('attribute edge cases', () => {
97
121
  const source = `{% key id=$user.name class=default($y, "test") %}Child{% /key %}`;
98
122
  const expected = `
@@ -105,6 +129,7 @@ describe('Formatter', () => {
105
129
 
106
130
  it('variables', () => {
107
131
  const source = `
132
+ {% tag "complex primary" /%}
108
133
  {% if $primary %}
109
134
  X
110
135
  {% /if %}
@@ -112,6 +137,8 @@ X
112
137
  {% key x=$user.name new=$flag /%}
113
138
  `;
114
139
  const expected = `
140
+ {% tag "complex primary" /%}
141
+
115
142
  {% if $primary %}
116
143
  X
117
144
  {% /if %}
@@ -472,4 +499,26 @@ Yes!
472
499
  check(source, expected);
473
500
  stable(expected);
474
501
  });
502
+
503
+ it('fences with block level tags', () => {
504
+ const source = `{% tab %}
505
+ \`\`\`json {% filename="package.json" %}
506
+ {
507
+ "dependencies": {
508
+ ...
509
+ {% highlight type="remove" %}
510
+ "beta": "1.2.3",
511
+ {% /highlight %}
512
+ {% highlight type="add" %}
513
+ "main": "1.2.4",
514
+ {% /highlight %}
515
+ ...
516
+ }
517
+ }
518
+ \`\`\`
519
+ {% /tab %}
520
+ `;
521
+
522
+ check(source, source);
523
+ });
475
524
  });
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) {
@@ -92,10 +103,12 @@ function* formatFunction(f: Function) {
92
103
  yield ')';
93
104
  }
94
105
 
95
- function* trimStart(g: Generator) {
106
+ function* trimStart(g: Generator<string>) {
96
107
  let n;
97
108
  do {
98
- n = g.next().value.trimStart();
109
+ const { value, done } = g.next();
110
+ if (done) return;
111
+ n = value.trimStart();
99
112
  } while (!n.length);
100
113
  yield n;
101
114
  yield* g;
@@ -183,7 +196,8 @@ function* formatNode(n: Node, o: Options = {}) {
183
196
  yield* formatAnnotations(n);
184
197
  yield NL;
185
198
  yield indent;
186
- yield* formatChildren(n, no);
199
+ // TODO use formatChildren once we can differentiate inline from block tags within fences
200
+ yield n.attributes.content; // yield* formatChildren(n, no);
187
201
  yield indent;
188
202
  yield '```';
189
203
  yield NL;
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
  }