@markdoc/markdoc 0.1.13 → 0.2.0

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.
Files changed (41) hide show
  1. package/README.md +9 -7
  2. package/dist/index.d.ts +3 -3
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +45 -29
  5. package/dist/index.js.map +3 -3
  6. package/dist/index.mjs +45 -29
  7. package/dist/index.mjs.map +3 -3
  8. package/dist/react.js +16 -5
  9. package/dist/react.js.map +3 -3
  10. package/dist/src/formatter.d.ts.map +1 -1
  11. package/dist/src/renderers/html.d.ts.map +1 -1
  12. package/dist/src/renderers/react/react.d.ts +1 -1
  13. package/dist/src/renderers/react/react.d.ts.map +1 -1
  14. package/dist/src/renderers/react/static.d.ts +1 -1
  15. package/dist/src/renderers/react/static.d.ts.map +1 -1
  16. package/dist/src/tag.d.ts +1 -0
  17. package/dist/src/tag.d.ts.map +1 -1
  18. package/dist/src/tags/conditional.d.ts +1 -1
  19. package/dist/src/tags/conditional.d.ts.map +1 -1
  20. package/dist/src/transformer.d.ts.map +1 -1
  21. package/dist/src/types.d.ts +1 -1
  22. package/dist/src/types.d.ts.map +1 -1
  23. package/dist/src/utils.d.ts +1 -1
  24. package/dist/src/utils.d.ts.map +1 -1
  25. package/index.ts +3 -3
  26. package/package.json +10 -3
  27. package/src/formatter.test.ts +70 -7
  28. package/src/formatter.ts +41 -12
  29. package/src/renderers/html.test.ts +10 -6
  30. package/src/renderers/html.ts +3 -2
  31. package/src/renderers/react/react.test.ts +5 -12
  32. package/src/renderers/react/react.ts +21 -4
  33. package/src/renderers/react/static.ts +21 -3
  34. package/src/tag.ts +4 -0
  35. package/src/tags/conditional.ts +11 -2
  36. package/src/transformer.ts +12 -2
  37. package/src/types.ts +1 -1
  38. package/src/utils.ts +2 -2
  39. package/dist/src/renderers/react/shared.d.ts +0 -5
  40. package/dist/src/renderers/react/shared.d.ts.map +0 -1
  41. package/src/renderers/react/shared.ts +0 -16
@@ -1,12 +1,28 @@
1
- import { tagName } from './shared';
2
- import type { createElement, Fragment, ReactNode } from 'react';
3
- import type { RenderableTreeNodes, Scalar } from '../../types';
1
+ import Tag from '../../tag';
2
+ import { RenderableTreeNodes, Scalar } from '../../types';
3
+
4
+ import type { createElement, ComponentType, Fragment, ReactNode } from 'react';
4
5
 
5
6
  type ReactShape = Readonly<{
6
7
  createElement: typeof createElement;
7
8
  Fragment: typeof Fragment;
8
9
  }>;
9
10
 
11
+ type Component = ComponentType<unknown>;
12
+
13
+ function tagName(
14
+ name: string,
15
+ components: Record<string, Component> | ((string: string) => Component)
16
+ ): string | Component {
17
+ return typeof name !== 'string'
18
+ ? name // This can be an object, e.g. when React.forwardRef is used
19
+ : name[0] !== name[0].toUpperCase()
20
+ ? name
21
+ : components instanceof Function
22
+ ? components(name)
23
+ : components[name];
24
+ }
25
+
10
26
  export default function dynamic(
11
27
  node: RenderableTreeNodes,
12
28
  React: ReactShape,
@@ -30,7 +46,8 @@ export default function dynamic(
30
46
  if (Array.isArray(node))
31
47
  return React.createElement(React.Fragment, null, ...node.map(render));
32
48
 
33
- if (node === null || typeof node !== 'object') return node;
49
+ if (node === null || typeof node !== 'object' || !Tag.isTag(node))
50
+ return node;
34
51
 
35
52
  const {
36
53
  name,
@@ -1,5 +1,22 @@
1
- import { tagName } from './shared';
2
- import type { RenderableTreeNode, RenderableTreeNodes } from '../../types';
1
+ import Tag from '../../tag';
2
+ import { RenderableTreeNode, RenderableTreeNodes } from '../../types';
3
+
4
+ import type { ComponentType } from 'react';
5
+
6
+ type Component = ComponentType<unknown>;
7
+
8
+ function tagName(
9
+ name: string,
10
+ components: Record<string, Component> | ((string: string) => Component)
11
+ ): string | Component {
12
+ return typeof name !== 'string'
13
+ ? 'Fragment'
14
+ : name[0] !== name[0].toUpperCase()
15
+ ? name
16
+ : components instanceof Function
17
+ ? components(name)
18
+ : components[name];
19
+ }
3
20
 
4
21
  function renderArray(children: RenderableTreeNode[]): string {
5
22
  return children.map(render).join(', ');
@@ -26,7 +43,8 @@ function render(node: RenderableTreeNodes): string {
26
43
  if (Array.isArray(node))
27
44
  return `React.createElement(React.Fragment, null, ${renderArray(node)})`;
28
45
 
29
- if (node === null || typeof node !== 'object') return JSON.stringify(node);
46
+ if (node === null || typeof node !== 'object' || !Tag.isTag(node))
47
+ return JSON.stringify(node);
30
48
 
31
49
  const {
32
50
  name,
package/src/tag.ts CHANGED
@@ -6,6 +6,10 @@ export default class Tag<
6
6
  > {
7
7
  readonly $$mdtype = 'Tag' as const;
8
8
 
9
+ static isTag = (tag: any): tag is Tag => {
10
+ return '$$mdtype' in tag && tag.$$mdtype === 'Tag';
11
+ };
12
+
9
13
  name: N;
10
14
  attributes: A;
11
15
  children: RenderableTreeNode[];
@@ -1,6 +1,13 @@
1
1
  import { isPromise } from '../utils';
2
2
 
3
- import type { Node, RenderableTreeNode, Schema, Value } from '../types';
3
+ import {
4
+ MaybePromise,
5
+ Node,
6
+ RenderableTreeNode,
7
+ RenderableTreeNodes,
8
+ Schema,
9
+ Value,
10
+ } from '../types';
4
11
 
5
12
  type Condition = { condition: Value; children: Node[] };
6
13
 
@@ -34,7 +41,9 @@ export const tagIf: Schema = {
34
41
  const conditions = renderConditions(node);
35
42
  for (const { condition, children } of conditions)
36
43
  if (truthy(condition)) {
37
- const nodes = children.flatMap((child) => child.transform(config));
44
+ const nodes = children.flatMap<MaybePromise<RenderableTreeNodes>>(
45
+ (child) => child.transform(config)
46
+ );
38
47
  if (nodes.some(isPromise)) {
39
48
  return Promise.all(nodes).then((nodes) => nodes.flat());
40
49
  }
@@ -2,7 +2,15 @@ import Tag from './tag';
2
2
  import { Class } from './schema-types/class';
3
3
  import { Id } from './schema-types/id';
4
4
  import { isPromise } from './utils';
5
- import type { Config, Node, NodeType, Schema, Transformer } from './types';
5
+ import type {
6
+ Config,
7
+ MaybePromise,
8
+ Node,
9
+ NodeType,
10
+ RenderableTreeNodes,
11
+ Schema,
12
+ Transformer,
13
+ } from './types';
6
14
 
7
15
  type AttributesSchema = Schema['attributes'];
8
16
 
@@ -43,7 +51,9 @@ export default {
43
51
  },
44
52
 
45
53
  children(node: Node, config: Config = {}) {
46
- const children = node.children.flatMap((child) => this.node(child, config));
54
+ const children = node.children.flatMap<MaybePromise<RenderableTreeNodes>>(
55
+ (child) => this.node(child, config)
56
+ );
47
57
  if (children.some(isPromise)) {
48
58
  return Promise.all(children);
49
59
  }
package/src/types.ts CHANGED
@@ -93,7 +93,7 @@ export type NodeType =
93
93
 
94
94
  export type Primitive = null | boolean | number | string;
95
95
 
96
- export type RenderableTreeNode = Tag | string | null;
96
+ export type RenderableTreeNode = Tag | Scalar;
97
97
  export type RenderableTreeNodes = RenderableTreeNode | RenderableTreeNode[];
98
98
 
99
99
  export type Scalar = Primitive | Scalar[] | { [key: string]: Scalar };
package/src/utils.ts CHANGED
@@ -15,8 +15,8 @@ export const CLOSE = '%}';
15
15
 
16
16
  export const IDENTIFIER_REGEX = /^[a-zA-Z0-9_-]+$/;
17
17
 
18
- export function isIdentifier(s: string): boolean {
19
- return IDENTIFIER_REGEX.test(s);
18
+ export function isIdentifier(s: any): s is string {
19
+ return typeof s === 'string' && IDENTIFIER_REGEX.test(s);
20
20
  }
21
21
 
22
22
  export function isPromise(a: any): a is Promise<any> {
@@ -1,5 +0,0 @@
1
- import type { ComponentType } from 'react';
2
- declare type Component = ComponentType<unknown>;
3
- export declare function tagName(name: string, components: Record<string, Component> | ((string: string) => Component)): string | Component;
4
- export {};
5
- //# sourceMappingURL=shared.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"shared.d.ts","sourceRoot":"","sources":["../../../../src/renderers/react/shared.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAE3C,aAAK,SAAS,GAAG,aAAa,CAAC,OAAO,CAAC,CAAC;AAExC,wBAAgB,OAAO,CACrB,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,MAAM,KAAK,SAAS,CAAC,GACtE,MAAM,GAAG,SAAS,CAQpB"}
@@ -1,16 +0,0 @@
1
- import type { ComponentType } from 'react';
2
-
3
- type Component = ComponentType<unknown>;
4
-
5
- export function tagName(
6
- name: string,
7
- components: Record<string, Component> | ((string: string) => Component)
8
- ): string | Component {
9
- return typeof name !== 'string'
10
- ? 'Fragment'
11
- : name[0] !== name[0].toUpperCase()
12
- ? name
13
- : components instanceof Function
14
- ? components(name)
15
- : components[name];
16
- }