sailfork / t3code-github / patches/ micromark-extension-llm-math@3.1.0.patch

diff --git a/dev/lib/math-flow.js b/dev/lib/math-flow.js
--- a/dev/lib/math-flow.js
+++ b/dev/lib/math-flow.js
@@ -4,7 +4,7 @@
 
 import {ok as assert} from 'devlop'
 import {factorySpace} from 'micromark-factory-space'
-import {markdownLineEnding} from 'micromark-util-character'
+import {markdownLineEnding, markdownSpace} from 'micromark-util-character'
 import {codes, constants, types} from 'micromark-util-symbol'
 
 /** @type {Construct} */
@@ -32,6 +32,9 @@
       ? tail[2].sliceSerialize(tail[1], true).length
       : 0
   let sizeOpen = 0
+  let closed = false
+  let slashes = 0
+  let trailingAfterClose = false
 
   return start
 
@@ -79,7 +82,32 @@
     }
 
     effects.exit('mathFlowFenceSequence')
-    return factorySpace(effects, metaBefore, types.whitespace)(code)
+
+    return factorySpace(effects, afterOpeningWhitespace, types.whitespace)(code)
+  }
+
+  /**
+   * After opening fence whitespace, check for same-line content.
+   *
+   * ```markdown
+   * > | $ 1 + 1 $
+   *        ^
+   * ```
+   *
+   * @type {State}
+   */
+  function afterOpeningWhitespace(code) {
+    if (code === codes.eof || markdownLineEnding(code)) {
+      return metaAfter(code)
+    }
+
+    effects.exit('mathFlowFence')
+
+    if (self.interrupt) {
+      return ok(code)
+    }
+
+    return beforeContentChunk(code)
   }
 
   /**
@@ -230,6 +258,14 @@
       )(code)
     }
 
+    if (code === codes.dollarSign) {
+      return effects.attempt(
+        {tokenize: tokenizeInlineClosingFence, partial: true},
+        after,
+        resumeContentAfterFailedClose
+      )(code)
+    }
+
     effects.enter('mathFlowValue')
     return contentChunk(code)
   }
@@ -247,13 +283,74 @@
    * @type {State}
    */
   function contentChunk(code) {
+    const escaped = slashes % 2 === 1
+    slashes = code === codes.backslash ? slashes + 1 : 0
     if (code === codes.eof || markdownLineEnding(code)) {
       effects.exit('mathFlowValue')
       return beforeContentChunk(code)
     }
 
+    if (code === codes.dollarSign && !escaped) {
+      effects.exit('mathFlowValue')
+      return effects.attempt(
+        {tokenize: tokenizeInlineClosingFence, partial: true},
+        after,
+        resumeContentAfterFailedClose
+      )(code)
+    }
+
     effects.consume(code)
     return contentChunk
+  }
+
+  /**
+   * Resume content after a failed inline closing fence attempt.
+   *
+   * @type {State}
+   */
+  function resumeContentAfterFailedClose(code) {
+    if (trailingAfterClose) return nok(code)
+    effects.enter('mathFlowValue')
+    effects.consume(code)
+    return contentChunk
+  }
+
+  /** @type {Tokenizer} */
+  function tokenizeInlineClosingFence(effects, ok, nok) {
+    let size = 0
+    return sequenceStart
+
+    function sequenceStart(code) {
+      effects.enter('mathFlowFence')
+      effects.enter('mathFlowFenceSequence')
+      return sequenceClose(code)
+    }
+
+    function sequenceClose(code) {
+      if (code === codes.dollarSign) {
+        size++
+        effects.consume(code)
+        return sequenceClose
+      }
+
+      if (size < sizeOpen) {
+        return nok(code)
+      }
+
+      effects.exit('mathFlowFenceSequence')
+      return factorySpace(effects, afterSequenceClose, types.whitespace)(code)
+    }
+
+    function afterSequenceClose(code) {
+      if (code === codes.eof || markdownLineEnding(code)) {
+        effects.exit('mathFlowFence')
+        closed = true
+        return ok(code)
+      }
+
+      trailingAfterClose = true
+      return nok(code)
+    }
   }
 
   /**
@@ -269,6 +366,7 @@
    * @type {State}
    */
   function after(code) {
+    if (!closed) return nok(code)
     effects.exit('mathFlow')
     return ok(code)
   }
@@ -357,6 +455,7 @@
     function afterSequenceClose(code) {
       if (code === codes.eof || markdownLineEnding(code)) {
         effects.exit('mathFlowFence')
+        closed = true
         return ok(code)
       }
 
diff --git a/dev/lib/math-tex-flow.js b/dev/lib/math-tex-flow.js
--- a/dev/lib/math-tex-flow.js
+++ b/dev/lib/math-tex-flow.js
@@ -26,6 +26,7 @@
  */
 function tokenizeMathFenced(effects, ok, nok) {
   const self = this
+  let closed = false
   const tail = self.events[self.events.length - 1]
   const initialSize =
     tail && tail[1].type === types.linePrefix
@@ -90,6 +91,7 @@
    * @type {State}
    */
   function metaAfter(code) {
+    if (code !== codes.eof && !markdownLineEnding(code)) return nok(code)
     effects.exit('mathFlowFence')
 
     if (self.interrupt) {
@@ -245,6 +247,7 @@
    * @type {State}
    */
   function after(code) {
+    if (!closed) return nok(code)
     effects.exit('mathFlow')
     return ok(code)
   }
@@ -327,6 +330,7 @@
     function afterSequenceClose(code) {
       if (code === codes.eof || markdownLineEnding(code)) {
         effects.exit('mathFlowFence')
+        closed = true
         return ok(code)
       }
 
diff --git a/dev/lib/math-tex-text.js b/dev/lib/math-tex-text.js
--- a/dev/lib/math-tex-text.js
+++ b/dev/lib/math-tex-text.js
@@ -25,6 +25,7 @@
   let token
   /** @type {number} */
   let openSequenceCode
+  let substantive = false
 
   return start
 
@@ -83,7 +84,7 @@
    * @type {State}
    */
   function between(code) {
-    if (code === codes.eof) {
+    if (code === codes.eof || code === codes.graveAccent || (self.mathSingleLine && markdownLineEnding(code))) {
       return nok(code)
     }
 
@@ -124,6 +125,8 @@
    * @type {State}
    */
   function data(code) {
+    if (code === codes.graveAccent) return nok(code)
+    if (code !== codes.backslash && code !== codes.eof && code !== codes.space && code !== codes.comma && !markdownLineEnding(code) && !(code >= codes.digit0 && code <= codes.digit9)) substantive = true
     if (
       code === codes.eof ||
       code === codes.space ||
@@ -155,6 +158,7 @@
         ? codes.rightParenthesis
         : codes.rightSquareBracket
     if (code === closeSequenceCode) {
+      if (openSequenceCode === codes.leftSquareBracket && !substantive) return nok(code)
       effects.consume(code)
       effects.exit('mathTextSequence')
       effects.exit('mathText')
@@ -180,5 +184,8 @@
  */
 function previous(code) {
   // If there is a previous code, there will always be a tail.
-  return code !== codes.backslash
+  return (
+    code !== codes.backslash ||
+    this.events[this.events.length - 1][1].type === types.characterEscape
+  )
 }
diff --git a/dev/lib/math-text.js b/dev/lib/math-text.js
--- a/dev/lib/math-text.js
+++ b/dev/lib/math-text.js
@@ -10 +10 @@
-import {markdownLineEnding} from 'micromark-util-character'
+import {markdownLineEnding, markdownLineEndingOrSpace} from 'micromark-util-character'
@@ -42,6 +42,13 @@
     let size
     /** @type {Token} */
     let token
+    /** @type {boolean} */
+    let openWhitespace
+    /** @type {number | undefined} */
+    let charBeforeClose
+    let slashes = 0
+    let previousData
+    let beforeData
 
     return start
 
@@ -89,6 +96,16 @@
       }
 
       effects.exit('mathTextSequence')
+      // Reject if opening is followed by line ending
+      if (markdownLineEnding(code)) {
+        return nok(code)
+      }
+      // Track if opening sequence is followed by whitespace
+      openWhitespace =
+        code === codes.space ||
+        code === codes.horizontalTab ||
+        code === codes.comma
+      if (sizeOpen === 1 && openWhitespace) return nok(code)
       return between(code)
     }
 
@@ -103,13 +120,14 @@
      * @type {State}
      */
     function between(code) {
-      if (code === codes.eof) {
+      if (code === codes.eof || code === codes.graveAccent || ((sizeOpen === 1 || self.mathSingleLine) && markdownLineEnding(code))) {
         return nok(code)
       }
 
       if (code === codes.dollarSign) {
         token = effects.enter('mathTextSequence')
         size = 0
+        charBeforeClose = self.previous
         return sequenceClose(code)
       }
 
@@ -144,6 +162,16 @@
      * @type {State}
      */
     function data(code) {
+      if (code === codes.graveAccent) return nok(code)
+      if (code === codes.dollarSign && slashes % 2 === 1) {
+        slashes = 0
+        effects.consume(code)
+        return data
+      }
+      if (code === codes.slash && previousData === codes.slash && beforeData === codes.colon) return nok(code)
+      beforeData = previousData
+      previousData = code
+      slashes = code === codes.backslash ? slashes + 1 : 0
       if (
         code === codes.eof ||
         code === codes.space ||
@@ -179,9 +207,29 @@
 
       // Done!
       if (size === sizeOpen) {
-        effects.exit('mathTextSequence')
-        effects.exit('mathText')
-        return ok(code)
+        // Reject if closing is preceded by line ending
+        if (markdownLineEnding(charBeforeClose)) {
+          return nok(code)
+        }
+        // Reject if closing is followed by a digit
+        if (code !== null && code >= codes.digit0 && code <= codes.digit9) {
+          return nok(code)
+        }
+        if (charBeforeClose === codes.comma && !openWhitespace && code !== codes.eof && !markdownLineEndingOrSpace(code) && !/[.,;:)\]}]/.test(String.fromCharCode(code))) {
+          return nok(code)
+        }
+        // Check if closing sequence has matching whitespace pattern
+        const closeWhitespace =
+          charBeforeClose === codes.space ||
+          charBeforeClose === codes.horizontalTab ||
+          (charBeforeClose === codes.comma && openWhitespace)
+        if (openWhitespace === closeWhitespace) {
+          effects.exit('mathTextSequence')
+          effects.exit('mathText')
+          return ok(code)
+        }
+        // Whitespace mismatch: mark as data
+        return nok(code)
       }
 
       // More or less accents: mark as data.
diff --git a/package.json b/package.json
--- a/package.json
+++ b/package.json
@@ -27,7 +27,7 @@
   "type": "module",
   "exports": {
     "development": "./dev/index.js",
-    "default": "./index.js"
+    "default": "./dev/index.js"
   },
   "files": [
     "dev/",