@markdoc/markdoc 0.5.9 → 0.5.11

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 (37) hide show
  1. package/dist/index.d.ts +1 -3
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +6524 -6330
  4. package/dist/index.js.map +4 -4
  5. package/dist/index.mjs +6493 -6299
  6. package/dist/index.mjs.map +4 -4
  7. package/dist/src/parser.d.ts +1 -2
  8. package/dist/src/parser.d.ts.map +1 -1
  9. package/dist/src/tokenizer/index.d.ts +3 -3
  10. package/dist/src/tokenizer/index.d.ts.map +1 -1
  11. package/dist/src/tokenizer/plugins/annotations.d.ts +2 -2
  12. package/dist/src/tokenizer/plugins/annotations.d.ts.map +1 -1
  13. package/dist/src/tokenizer/plugins/comments.d.ts +2 -2
  14. package/dist/src/tokenizer/plugins/comments.d.ts.map +1 -1
  15. package/dist/src/tokenizer/plugins/frontmatter.d.ts +2 -2
  16. package/dist/src/tokenizer/plugins/frontmatter.d.ts.map +1 -1
  17. package/dist/src/tokenizer/plugins/link.d.ts +2 -2
  18. package/dist/src/tokenizer/plugins/link.d.ts.map +1 -1
  19. package/dist/src/types.d.ts +22 -0
  20. package/dist/src/types.d.ts.map +1 -1
  21. package/dist/src/utils.d.ts +1 -1
  22. package/dist/src/utils.d.ts.map +1 -1
  23. package/index.ts +7 -3
  24. package/package.json +4 -8
  25. package/src/formatter.test.ts +55 -0
  26. package/src/formatter.ts +5 -5
  27. package/src/grammar/tag.d.ts +3 -1
  28. package/src/parser.ts +19 -13
  29. package/src/tokenizer/index.ts +7 -7
  30. package/src/tokenizer/plugins/annotations.test.ts +126 -0
  31. package/src/tokenizer/plugins/annotations.ts +33 -16
  32. package/src/tokenizer/plugins/comments.ts +4 -6
  33. package/src/tokenizer/plugins/frontmatter.ts +5 -6
  34. package/src/tokenizer/plugins/link.ts +9 -11
  35. package/src/types.ts +23 -0
  36. package/src/utils.ts +1 -1
  37. package/global.d.ts +0 -18
@@ -1,19 +1,28 @@
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 MarkdownIt from 'markdown-it';
8
+
9
+ type StateBlock = MarkdownIt.StateBlock;
10
+ type StateInline = MarkdownIt.StateInline;
11
+ type StateCore = MarkdownIt.StateCore;
12
+ type StateWithDelimiters = (StateBlock | StateInline) & {
13
+ delimiters?: MarkdownIt.Delimiter[];
14
+ };
15
+ // FenceToken combines markdown-it and Markdoc token types, using the more
16
+ // specific markdown-it type for fields it defines (e.g. `info`, `content`,
17
+ // `tag`), and Markdoc's own type for fields it overrides at runtime
18
+ // (`errors`, `children`, `meta`). Previously we used module augmentation
19
+ // (global.d.ts) to extend markdown-it's Token directly, but markdown-it 15
20
+ // no longer exposes an augmentable module path for it.
21
+ type FenceToken = Omit<MarkdownIt.Token, 'children' | 'meta'> &
22
+ Pick<Token, 'errors' | 'children' | 'meta'>;
14
23
 
15
24
  function createToken(
16
- state: StateBlock | StateInline,
25
+ state: StateWithDelimiters,
17
26
  content: string,
18
27
  contentStart?: number
19
28
  ): Token {
@@ -65,10 +74,16 @@ function block(
65
74
  if (!tagEnd || tagEnd < lastPossible - CLOSE.length) return false;
66
75
 
67
76
  const contentStart = start + OPEN.length;
68
- const content = state.src.slice(contentStart, tagEnd).trim();
69
77
  const lines = state.src
70
78
  .slice(start, tagEnd + CLOSE.length)
71
79
  .split('\n').length;
80
+ const text = state.getLines(startLine, startLine + lines, 0, false);
81
+ const contentEnd = findTagEnd(text);
82
+ if (contentEnd === null) return false;
83
+
84
+ const content = text
85
+ .slice(text.indexOf(OPEN) + OPEN.length, contentEnd)
86
+ .trim();
72
87
 
73
88
  if (content[0] === '$') return false;
74
89
 
@@ -94,14 +109,16 @@ function inline(state: StateInline, silent: boolean): boolean {
94
109
  }
95
110
 
96
111
  function core(state: StateCore) {
97
- let token: Token;
98
- for (token of state.tokens) {
99
- if (token.type !== 'fence') continue;
112
+ for (const raw of state.tokens) {
113
+ if (raw.type !== 'fence') continue;
114
+
115
+ const token: FenceToken = raw;
116
+ if (!token.map) continue;
100
117
 
101
118
  if (token.info.includes(OPEN)) {
102
119
  const start = token.info.indexOf(OPEN);
103
120
  const end = findTagEnd(token.info, start);
104
- const content = token.info.slice(start + OPEN.length, end);
121
+ const content = token.info.slice(start + OPEN.length, end ?? undefined);
105
122
 
106
123
  try {
107
124
  const { meta } = parse(content.trim(), { Variable, Function });
@@ -130,7 +147,7 @@ function core(state: StateCore) {
130
147
  }
131
148
  }
132
149
 
133
- export default function plugin(md: MarkdownIt /* options */) {
150
+ export default function plugin(md: MarkdownIt.MarkdownIt /* options */) {
134
151
  md.block.ruler.before('paragraph', 'annotations', block, {
135
152
  alt: ['paragraph', 'blockquote'],
136
153
  });
@@ -1,12 +1,10 @@
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 from 'markdown-it';
4
2
 
5
3
  const OPEN = '<!--';
6
4
  const CLOSE = '-->';
7
5
 
8
6
  function block(
9
- state: StateBlock,
7
+ state: MarkdownIt.StateBlock,
10
8
  startLine: number,
11
9
  endLine: number,
12
10
  silent: boolean
@@ -29,7 +27,7 @@ function block(
29
27
  return true;
30
28
  }
31
29
 
32
- function inline(state: StateInline, silent: boolean): boolean {
30
+ function inline(state: MarkdownIt.StateInline, silent: boolean): boolean {
33
31
  if (!state.src.startsWith(OPEN, state.pos)) return false;
34
32
 
35
33
  const close = state.src.indexOf(CLOSE, state.pos);
@@ -45,7 +43,7 @@ function inline(state: StateInline, silent: boolean): boolean {
45
43
  return true;
46
44
  }
47
45
 
48
- export default function plugin(md: MarkdownIt) {
46
+ export default function plugin(md: MarkdownIt.MarkdownIt) {
49
47
  md.block.ruler.before('table', 'comment', block, { alt: ['paragraph'] });
50
48
  md.inline.ruler.push('comment', inline);
51
49
  }
@@ -1,19 +1,18 @@
1
- import type MarkdownIt from 'markdown-it/lib';
2
- import type StateBlock from 'markdown-it/lib/rules_block/state_block';
1
+ import type MarkdownIt from 'markdown-it';
3
2
 
4
3
  const fence = '---';
5
4
 
6
- function getLine(state: StateBlock, n: number) {
5
+ function getLine(state: MarkdownIt.StateBlock, n: number) {
7
6
  return state.src.slice(state.bMarks[n], state.eMarks[n]).trim();
8
7
  }
9
8
 
10
- function findClose(state: StateBlock, endLine: number) {
9
+ function findClose(state: MarkdownIt.StateBlock, endLine: number) {
11
10
  for (let line = 1; line < endLine; line++)
12
11
  if (getLine(state, line) === fence) return line;
13
12
  }
14
13
 
15
14
  function block(
16
- state: StateBlock,
15
+ state: MarkdownIt.StateBlock,
17
16
  startLine: number,
18
17
  endLine: number,
19
18
  silent: boolean
@@ -34,6 +33,6 @@ function block(
34
33
  return true;
35
34
  }
36
35
 
37
- export default function plugin(md: MarkdownIt /* options */) {
36
+ export default function plugin(md: MarkdownIt.MarkdownIt /* options */) {
38
37
  md.block.ruler.before('hr', 'frontmatter', block);
39
38
  }
@@ -1,7 +1,5 @@
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 from 'markdown-it';
2
+ import type { Token } from '../../types';
5
3
 
6
4
  import { containsMarkdocTagInUrl } from '../../utils';
7
5
  import { LinkPluginOptions } from '..';
@@ -9,19 +7,16 @@ import { LinkPluginOptions } from '..';
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,
21
16
  });
22
17
  }
23
18
 
24
- function core(state: StateCore, options: LinkPluginOptions) {
19
+ function core(state: MarkdownIt.StateCore, options: LinkPluginOptions) {
25
20
  let token: Token;
26
21
  for (token of state.tokens) {
27
22
  if (token.type !== 'inline' || typeof token.content !== 'string') continue;
@@ -32,6 +27,9 @@ function core(state: StateCore, options: LinkPluginOptions) {
32
27
  }
33
28
  }
34
29
 
35
- export default function plugin(md: MarkdownIt, options: LinkPluginOptions) {
30
+ export default function plugin(
31
+ md: MarkdownIt.MarkdownIt,
32
+ options: LinkPluginOptions
33
+ ) {
36
34
  md.core.ruler.push('link_url_validation', (state) => core(state, options));
37
35
  }
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/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
- }