tarsec 0.3.0 → 0.3.2

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.
@@ -3,20 +3,22 @@ export * from "./inline.js";
3
3
  export * from "./blocks.js";
4
4
  export * from "./references.js";
5
5
  export * from "./frontmatter.js";
6
- import { seq, sepBy, or, optional, many1, map } from "../../combinators.js";
6
+ import { seq, or, optional, many, capture, seqC, map } from "../../combinators.js";
7
7
  import { spaces, newline } from "../../parsers.js";
8
8
  import { headingParser, codeBlockParser, blockQuoteParser, paragraphParser, imageParser, horizontalRuleParser, setextHeadingParser, indentedCodeBlockParser, listParser, tableParser, htmlBlockParser, } from "./blocks.js";
9
9
  import { linkDefinitionParser, footnoteDefinitionParser, resolveReferences, } from "./references.js";
10
10
  import { frontmatterParser } from "./frontmatter.js";
11
- // Block separator: one or more newlines (with optional trailing horizontal
12
- // whitespace). Crucially this does NOT consume leading indentation on the
13
- // next block so a 4-space indented code block isn't dewhitespaced before
14
- // indentedCodeBlockParser ever sees it.
15
- const blockSeparator = many1(newline);
11
+ const blockAlt = or(setextHeadingParser, horizontalRuleParser, headingParser, codeBlockParser, indentedCodeBlockParser, tableParser, blockQuoteParser, listParser, htmlBlockParser, linkDefinitionParser, footnoteDefinitionParser, paragraphParser, imageParser);
12
+ // A block followed by zero-or-more trailing newlines. Blocks differ in whether
13
+ // they consume their own terminating "\n" (e.g. headingParser does, codeBlock
14
+ // doesn't), so we can't use sepBy(many1(newline), block) — it would fail to
15
+ // separate two blocks when the first already ate its newline (e.g. a heading
16
+ // directly followed by a list with no intervening blank line).
17
+ const blockEntry = map(seqC(capture(blockAlt, "b"), many(newline)), ({ b }) => b);
16
18
  const _markdownParser = seq([
17
19
  optional(frontmatterParser),
18
20
  optional(spaces),
19
- sepBy(blockSeparator, or(setextHeadingParser, horizontalRuleParser, headingParser, codeBlockParser, indentedCodeBlockParser, tableParser, blockQuoteParser, listParser, htmlBlockParser, linkDefinitionParser, footnoteDefinitionParser, paragraphParser, imageParser)),
21
+ many(blockEntry),
20
22
  optional(spaces),
21
23
  ], (r) => {
22
24
  const fm = r[0];
@@ -2,11 +2,17 @@ import { seqC, seqR, capture, captureCaptures, or, not, map, many, many1, many1T
2
2
  import { str, char, eof, set, oneOf, alphanum, noneOf, digit, letter, anyChar } from "../../parsers.js";
3
3
  import { success, failure } from "../../types.js";
4
4
  import { optional, between } from "../../combinators.js";
5
- // Stop inline-text at any single delimiter char OR at a hard-break sequence
6
- // (" \n"+). Using many1Till with an `or` of delimiters makes the stop set
7
- // composable rather than embedded inside a regex. `]` is included so that
8
- // inline-text inside a link-text (`[...]`) terminates at the closing `]`.
9
- const inlineTextStop = or(oneOf("*_`[]!<~\\&\n"), str(" "));
5
+ // The "two-or-more trailing spaces then `\n`" half of a hard break. Shared
6
+ // between `hardBreakParser` (which emits the hard-break node) and
7
+ // `inlineTextStop` (which uses it to know where to stop). Keeping these in
8
+ // sync matters: if the run pattern here disagreed with what `hardBreakParser`
9
+ // accepts, inline-text could either swallow a real hard break or — as the
10
+ // previous `str(" ")` did — stop on any incidental double-space (e.g. a
11
+ // 2-space line indent inside a list-item continuation) and freeze the
12
+ // surrounding paragraph at zero progress.
13
+ const hardBreakSpaces = seqR(str(" "), many(char(" ")), char("\n"));
14
+ // `]` is included so inline-text inside a link-text (`[...]`) ends at the `]`.
15
+ const inlineTextStop = or(oneOf("*_`[]!<~\\&\n"), hardBreakSpaces);
10
16
  export const inlineTextParser = map(many1Till(inlineTextStop), (content) => ({ type: "inline-text", content }));
11
17
  /**
12
18
  * Run `inlineMarkdownParser` repeatedly until `stop` would match at the
@@ -219,7 +225,7 @@ export const imageParser = map(seqC(str("!["), capture(iManyTillStr("]("), "alt"
219
225
  });
220
226
  export const hardBreakParser = map(or(
221
227
  // two-or-more trailing spaces then newline
222
- seqR(str(" "), many(char(" ")), char("\n")),
228
+ hardBreakSpaces,
223
229
  // backslash then newline
224
230
  seqR(char("\\"), char("\n"))), () => ({ type: "inline-hard-break" }));
225
231
  /** A single `\n` that is *not* part of a blank line (which would terminate the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tarsec",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "A parser combinator library for TypeScript, inspired by Parsec.",
5
5
  "homepage": "https://github.com/egonSchiele/tarsec",
6
6
  "scripts": {