markdown2typst 0.1.7 → 0.1.9

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markdown2typst",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "A comprehensive JavaScript library for converting Markdown to Typst code",
5
5
  "type": "module",
6
6
  "main": "./dist/markdown2typst.js",
package/src/parser.ts CHANGED
@@ -28,6 +28,12 @@ import { ErrorSeverity } from './types.js';
28
28
  */
29
29
  export function parseMarkdown(markdown: string, onError?: ErrorCallback): Root {
30
30
  try {
31
+ // remark-math requires a blank line before display math. Accept a
32
+ // standalone $$ block immediately after a paragraph as well.
33
+ const normalizedMarkdown = markdown.replace(
34
+ /([^\n])\n(\$\$)/g,
35
+ '$1\n\n$2'
36
+ );
31
37
  const processor = unified()
32
38
  .use(remarkParse)
33
39
  .use(remarkFrontmatter, ['yaml'])
@@ -35,7 +41,7 @@ export function parseMarkdown(markdown: string, onError?: ErrorCallback): Root {
35
41
  .use(remarkMath)
36
42
  .use(remarkFixAdjacentMath);
37
43
 
38
- const parsedTree = processor.parse(markdown);
44
+ const parsedTree = processor.parse(normalizedMarkdown);
39
45
  const tree = processor.runSync(parsedTree) as Root;
40
46
 
41
47
  return tree;
@@ -1,6 +1,8 @@
1
1
  import { visit } from 'unist-util-visit';
2
2
  import { unified } from 'unified';
3
3
  import remarkParse from 'remark-parse';
4
+ import remarkMath from 'remark-math';
5
+ import remarkGfm from 'remark-gfm';
4
6
 
5
7
  /**
6
8
  * Remark plugin to fix math blocks that swallowed adjacent text because
@@ -35,13 +37,20 @@ export function remarkFixAdjacentMath() {
35
37
 
36
38
  // We need to append the remainder as new parsed markdown nodes after this node
37
39
  if (remainder.trim().length > 0) {
38
- // Parse the remainder
39
- const processor = unified().use(remarkParse);
40
+ // Parse the remainder with full features
41
+ const processor = unified()
42
+ .use(remarkParse)
43
+ .use(remarkGfm, { singleTilde: false })
44
+ .use(remarkMath)
45
+ // Notice we also recursively apply this plugin in case the remainder has more adjacent math
46
+ .use(remarkFixAdjacentMath);
47
+
40
48
  const remainderAst = processor.parse(remainder.trim());
49
+ const remainderTree = processor.runSync(remainderAst);
41
50
 
42
51
  // Store the nodes to be inserted
43
52
  node.data = node.data || {};
44
- node.data.insertAfter = remainderAst.children;
53
+ node.data.insertAfter = (remainderTree as any).children;
45
54
  }
46
55
  } else {
47
56
  node.value = value.trim();