@markdoc/markdoc 0.5.10 → 0.5.12

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 (43) hide show
  1. package/README.md +20 -13
  2. package/dist/index.d.ts +22 -23
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +6528 -6331
  5. package/dist/index.js.map +4 -4
  6. package/dist/index.mjs +6498 -6301
  7. package/dist/index.mjs.map +4 -4
  8. package/dist/react.js +2 -1
  9. package/dist/react.js.map +2 -2
  10. package/dist/src/parser.d.ts +1 -2
  11. package/dist/src/parser.d.ts.map +1 -1
  12. package/dist/src/renderers/react/react.d.ts.map +1 -1
  13. package/dist/src/tokenizer/index.d.ts +17 -8
  14. package/dist/src/tokenizer/index.d.ts.map +1 -1
  15. package/dist/src/tokenizer/plugins/annotations.d.ts +1 -1
  16. package/dist/src/tokenizer/plugins/annotations.d.ts.map +1 -1
  17. package/dist/src/tokenizer/plugins/comments.d.ts +1 -1
  18. package/dist/src/tokenizer/plugins/comments.d.ts.map +1 -1
  19. package/dist/src/tokenizer/plugins/frontmatter.d.ts +1 -1
  20. package/dist/src/tokenizer/plugins/frontmatter.d.ts.map +1 -1
  21. package/dist/src/tokenizer/plugins/link.d.ts +2 -2
  22. package/dist/src/tokenizer/plugins/link.d.ts.map +1 -1
  23. package/dist/src/types.d.ts +22 -0
  24. package/dist/src/types.d.ts.map +1 -1
  25. package/dist/src/utils.d.ts +1 -1
  26. package/dist/src/utils.d.ts.map +1 -1
  27. package/index.ts +8 -3
  28. package/package.json +34 -48
  29. package/src/grammar/tag.d.ts +1 -1
  30. package/src/parser.ts +19 -13
  31. package/src/renderers/react/react.ts +8 -6
  32. package/src/renderers/react/static.ts +4 -4
  33. package/src/tag.ts +1 -1
  34. package/src/tokenizer/index.ts +27 -14
  35. package/src/tokenizer/plugins/annotations.ts +33 -15
  36. package/src/tokenizer/plugins/comments.ts +1 -3
  37. package/src/tokenizer/plugins/frontmatter.ts +1 -2
  38. package/src/tokenizer/plugins/link.ts +5 -10
  39. package/src/types.ts +23 -0
  40. package/src/utils.ts +1 -1
  41. package/src/validator.ts +2 -2
  42. package/global.d.ts +0 -18
  43. package/spec/marktest/index.ts +0 -136
@@ -1,19 +1,32 @@
1
- import { findTagEnd, parseTags } from '../../utils';
1
+ import { findTagEnd, OPEN, CLOSE, parseTags } from '../../utils';
2
2
  import { parse, SyntaxError } from '../../grammar/tag';
3
3
  import Variable from '../../ast/variable';
4
4
  import Function from '../../ast/function';
5
5
 
6
- import type { AttributeValue } from '../../types';
7
- import type MarkdownIt from 'markdown-it/lib';
8
- import type StateCore from 'markdown-it/lib/rules_core/state_core';
9
- import type StateInline from 'markdown-it/lib/rules_inline/state_inline';
10
- import type StateBlock from 'markdown-it/lib/rules_block/state_block';
11
- import type Token from 'markdown-it/lib/token';
12
-
13
- import { OPEN, CLOSE } from '../../utils';
6
+ import type { AttributeValue, Token } from '../../types';
7
+ import type {
8
+ Delimiter,
9
+ MarkdownIt,
10
+ StateBlock,
11
+ StateInline,
12
+ StateCore,
13
+ Token as MarkdownItToken,
14
+ } from 'markdown-it';
15
+
16
+ type StateWithDelimiters = (StateBlock | StateInline) & {
17
+ delimiters?: Delimiter[];
18
+ };
19
+ // FenceToken combines markdown-it and Markdoc token types, using the more
20
+ // specific markdown-it type for fields it defines (e.g. `info`, `content`,
21
+ // `tag`), and Markdoc's own type for fields it overrides at runtime
22
+ // (`errors`, `children`, `meta`). Previously we used module augmentation
23
+ // (global.d.ts) to extend markdown-it's Token directly, but markdown-it 15
24
+ // no longer exposes an augmentable module path for it.
25
+ type FenceToken = Omit<MarkdownItToken, 'children' | 'meta'> &
26
+ Pick<Token, 'errors' | 'children' | 'meta'>;
14
27
 
15
28
  function createToken(
16
- state: StateBlock | StateInline,
29
+ state: StateWithDelimiters,
17
30
  content: string,
18
31
  contentStart?: number
19
32
  ): Token {
@@ -69,8 +82,11 @@ function block(
69
82
  .slice(start, tagEnd + CLOSE.length)
70
83
  .split('\n').length;
71
84
  const text = state.getLines(startLine, startLine + lines, 0, false);
85
+ const contentEnd = findTagEnd(text);
86
+ if (contentEnd === null) return false;
87
+
72
88
  const content = text
73
- .slice(text.indexOf(OPEN) + OPEN.length, findTagEnd(text))
89
+ .slice(text.indexOf(OPEN) + OPEN.length, contentEnd)
74
90
  .trim();
75
91
 
76
92
  if (content[0] === '$') return false;
@@ -97,14 +113,16 @@ function inline(state: StateInline, silent: boolean): boolean {
97
113
  }
98
114
 
99
115
  function core(state: StateCore) {
100
- let token: Token;
101
- for (token of state.tokens) {
102
- if (token.type !== 'fence') continue;
116
+ for (const raw of state.tokens) {
117
+ if (raw.type !== 'fence') continue;
118
+
119
+ const token: FenceToken = raw;
120
+ if (!token.map) continue;
103
121
 
104
122
  if (token.info.includes(OPEN)) {
105
123
  const start = token.info.indexOf(OPEN);
106
124
  const end = findTagEnd(token.info, start);
107
- const content = token.info.slice(start + OPEN.length, end);
125
+ const content = token.info.slice(start + OPEN.length, end ?? undefined);
108
126
 
109
127
  try {
110
128
  const { meta } = parse(content.trim(), { Variable, Function });
@@ -1,6 +1,4 @@
1
- import type MarkdownIt from 'markdown-it/lib';
2
- import type StateBlock from 'markdown-it/lib/rules_block/state_block';
3
- import type StateInline from 'markdown-it/lib/rules_inline/state_inline';
1
+ import type { MarkdownIt, StateBlock, StateInline } from 'markdown-it';
4
2
 
5
3
  const OPEN = '<!--';
6
4
  const CLOSE = '-->';
@@ -1,5 +1,4 @@
1
- import type MarkdownIt from 'markdown-it/lib';
2
- import type StateBlock from 'markdown-it/lib/rules_block/state_block';
1
+ import type { MarkdownIt, StateBlock } from 'markdown-it';
3
2
 
4
3
  const fence = '---';
5
4
 
@@ -1,20 +1,15 @@
1
- import type MarkdownIt from 'markdown-it/lib';
2
- import type StateCore from 'markdown-it/lib/rules_core/state_core';
3
- import type Token from 'markdown-it/lib/token';
4
- import type { ValidationError } from '../../types';
1
+ import type { MarkdownIt, StateCore } from 'markdown-it';
2
+ import type { Token } from '../../types';
5
3
 
6
4
  import { containsMarkdocTagInUrl } from '../../utils';
7
- import { LinkPluginOptions } from '..';
5
+ import type { LinkPluginOptions } from '..';
8
6
 
9
7
  const INVALID_HREF_MESSAGE =
10
8
  "The 'href' format cannot contain Markdoc tag or variable. URLs must be static strings.";
11
9
 
12
- type TokenWithErrors = Token & { errors?: ValidationError[] };
13
-
14
10
  function pushHrefError(token: Token) {
15
- const tokenWithErrors = token as TokenWithErrors;
16
- if (!tokenWithErrors.errors) tokenWithErrors.errors = [];
17
- tokenWithErrors.errors.push({
11
+ if (!token.errors) token.errors = [];
12
+ token.errors.push({
18
13
  id: 'href-format-invalid',
19
14
  level: 'error',
20
15
  message: INVALID_HREF_MESSAGE,
package/src/types.ts CHANGED
@@ -146,6 +146,29 @@ export type ValidationError = {
146
146
  location?: Location;
147
147
  };
148
148
 
149
+ // Markdoc's own loose token shape. It's compatible with markdown-it's real
150
+ // `Token` class, but also covers the lightweight pseudo-tokens `parseTags`
151
+ // builds for inline tag content, which aren't real markdown-it tokens.
152
+ export type Token = {
153
+ type: string;
154
+ tag?: string;
155
+ attrs?: Array<[string, string | number]> | null;
156
+ map?: number[] | null;
157
+ nesting?: -1 | 0 | 1;
158
+ level?: number;
159
+ children?: Token[] | null;
160
+ content?: string;
161
+ markup?: string;
162
+ info?: string;
163
+ meta?: Record<string, any> | null;
164
+ block?: boolean;
165
+ hidden?: boolean;
166
+ errors?: ValidationError[];
167
+ position?: { start?: number; end?: number };
168
+ start?: number;
169
+ end?: number;
170
+ };
171
+
149
172
  export type ValidateError = {
150
173
  type: string;
151
174
  lines: number[];
package/src/utils.ts CHANGED
@@ -2,7 +2,7 @@ import { parse, SyntaxError } from './grammar/tag';
2
2
  import Variable from './ast/variable';
3
3
  import Function from './ast/function';
4
4
 
5
- import type Token from 'markdown-it/lib/token';
5
+ import type { Token } from './types';
6
6
 
7
7
  enum STATES {
8
8
  normal,
package/src/validator.ts CHANGED
@@ -36,8 +36,8 @@ export function validateType(
36
36
  return !schema?.returns
37
37
  ? true
38
38
  : Array.isArray(schema.returns)
39
- ? schema.returns.find((t) => t === type) !== undefined
40
- : schema.returns === type;
39
+ ? schema.returns.find((t) => t === type) !== undefined
40
+ : schema.returns === type;
41
41
  }
42
42
 
43
43
  if (Ast.isAst(value)) return true;
package/global.d.ts DELETED
@@ -1,18 +0,0 @@
1
- import { Delimiter } from 'markdown-it/lib/rules_inline/state_inline';
2
- import type { ValidationError } from './src/types';
3
-
4
- declare module 'markdown-it/lib/token' {
5
- export default class Token extends require('markdown-it/lib/token') {
6
- errors?: ValidationError[];
7
- position?: {
8
- start?: number;
9
- end?: number;
10
- };
11
- }
12
- }
13
-
14
- declare module 'markdown-it/lib/rules_block/state_block' {
15
- export default class Token extends require('markdown-it/lib/rules_block/state_block') {
16
- delimiters?: Delimiter[];
17
- }
18
- }
@@ -1,136 +0,0 @@
1
- import { promises as fs } from 'fs';
2
- import { resolve } from 'path';
3
- import { diffJson, diffChars } from 'diff';
4
- import yaml from 'yaml-js';
5
-
6
- import markdoc from '../../index';
7
- import React from './react-shim';
8
-
9
- class Loader extends yaml.loader.Loader {
10
- construct_mapping(node) {
11
- return {
12
- ...super.construct_mapping(node),
13
- $$lines: {
14
- start: node.start_mark.line,
15
- end: node.end_mark.line,
16
- },
17
- };
18
- }
19
- }
20
-
21
- const tokenizer = new markdoc.Tokenizer({
22
- allowIndentation: true,
23
- allowComments: true,
24
- });
25
-
26
- function parse(content: string, slots?: boolean, file?: string) {
27
- const tokens = tokenizer.tokenize(content);
28
- return markdoc.parse(tokens, { file, slots });
29
- }
30
-
31
- function stripLines(object) {
32
- const removeLines = (key, value) => (key === '$$lines' ? undefined : value);
33
- return JSON.parse(JSON.stringify(object, removeLines));
34
- }
35
-
36
- function render(code, config, dynamic) {
37
- const partials = {};
38
- for (const [file, content] of Object.entries(config.partials ?? {}))
39
- partials[file] = parse(content as string, false, file);
40
-
41
- const { react, reactStatic } = markdoc.renderers;
42
- const transformed = markdoc.transform(code, { ...config, partials });
43
- return dynamic ? react(transformed, React) : eval(reactStatic(transformed));
44
- }
45
-
46
- function checkMatch(diffs) {
47
- return diffs.find((diff) => diff.added || diff.removed);
48
- }
49
-
50
- function run(
51
- { code = null, renderer = 'react', config = {}, expected = undefined } = {},
52
- dynamic
53
- ) {
54
- if (renderer === 'html') {
55
- const transformed = markdoc.transform(code, config);
56
- const output = markdoc.renderers.html(transformed);
57
- const diff = diffChars((expected || '').trim(), (output || '').trim());
58
- return checkMatch(diff) ? diff : false;
59
- }
60
-
61
- const output = render(code, config, dynamic) || {};
62
- const diff = diffJson(expected || {}, output.children || []);
63
- return checkMatch(diff) ? diff : false;
64
- }
65
-
66
- function formatter(filename, { $$lines: lines, name, code }, diff) {
67
- const prettyDiff = diff
68
- .flatMap((part) => [
69
- part.added ? '\x1b[32m' : part.removed ? '\x1b[31m' : '\x1b[0m',
70
- part.added || part.removed ? part.value.replace(/ /g, '⎵') : part.value,
71
- ])
72
- .join('');
73
-
74
- return [
75
- `\x1b[31mFAILED:\x1b[0m ${filename}:${lines.start}`,
76
- name,
77
- code,
78
- prettyDiff,
79
- ].join('\n\n');
80
- }
81
-
82
- function formatValidation(filename, test, validation) {
83
- const { $$lines: lines, name, code } = test;
84
-
85
- let output = '';
86
- for (const {
87
- lines,
88
- error: { message },
89
- } of validation)
90
- output += `${lines ? lines[0] : '?'}:${message}\n`;
91
-
92
- return [
93
- `\x1b[31mINVALID:\x1b[0m ${filename}:${lines.start}`,
94
- name,
95
- code,
96
- output,
97
- ].join('\n\n');
98
- }
99
-
100
- (async () => {
101
- const [filename, line] = process.argv.slice(2).join('').split(':');
102
- const path = resolve(filename);
103
- const file = await fs.readFile(path);
104
- const tests = yaml.load(file, Loader);
105
-
106
- let exitCode = 0;
107
- for (const test of tests) {
108
- const code = parse(test.code || '', test.slots);
109
-
110
- const { start, end } = test.$$lines;
111
- if (line && (Number(line) - 1 < start || Number(line) - 1 > end)) continue;
112
-
113
- const validation = markdoc.validate(code, test.config);
114
-
115
- let result;
116
- if (test.expectedError) {
117
- const output = validation.map((v) => v.error.message).join('\n');
118
- const diff = diffChars(
119
- (test.expectedError || '').trim(),
120
- (output || '').trim()
121
- );
122
-
123
- result = checkMatch(diff) ? diff : false;
124
- } else {
125
- if (validation.length && test.validation != false)
126
- console.log(formatValidation(path, test, validation));
127
-
128
- result = run({ ...stripLines(test), code }, true);
129
- }
130
-
131
- if (!result) continue;
132
- console.log(formatter(path, test, result));
133
- exitCode = 1;
134
- }
135
- process.exit(exitCode);
136
- })();