@mulmoclaude/markdown-utils 2.0.0 → 2.1.0

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.
@@ -32,8 +32,29 @@
32
32
  // and must not be a backslash.
33
33
  // 4. The character AFTER the closing `$` must not be an ASCII digit
34
34
  // (`$5-$10`).
35
- // 5. The body must be non-empty, single-line, and must not consist
36
- // only of digits and separators (`$1,000$`).
35
+ // 5. The body must be non-empty, single-line, and must be something a
36
+ // formula could be ABOUT: a number written the way money is
37
+ // (`$1,000$`, `$1.000,50$` — a price written twice) or separators
38
+ // with no digits at all (`$+$`) are not.
39
+ //
40
+ // Rule 5 used to reject EVERY digits-and-separators body, and that was
41
+ // too wide: `1秒を $10000$ 個のステップに割る` and `答えは $1$` are the
42
+ // ordinary way to write a number in a maths article, and both came out
43
+ // as a literal `$10000$` / `$1$` sitting in the prose. The signature of
44
+ // a price is its SHAPE — digits in threes, or more than one separator —
45
+ // and the two shapes that actually
46
+ // appear in currency prose are already dead: `$100 と $200` by rule 3
47
+ // (whitespace before the close) and `$5-$10` by rule 4 (a digit after
48
+ // it). What stays admitted is a body like `$5$`, which a person quoting
49
+ // a price does not write: they write `$5`, and it is the DOUBLED
50
+ // delimiter that makes it maths.
51
+ //
52
+ // The SHAPE is matched rather than a particular separator, because a
53
+ // comma groups thousands in English and marks the decimal in most of
54
+ // Europe, and a dot does the opposite (codex, #2985). `$1,5$` is one and
55
+ // a half and typesets; `$1.000,50$` is a price and does not. `$1,500$`
56
+ // is genuinely ambiguous and is read as the price — the reading rule 5
57
+ // already had, which this narrowing keeps rather than reverses.
37
58
  //
38
59
  // Rules 2-5 are enforced in the tokenizer, where the whole match is in
39
60
  // hand. Rule 1 needs the character BEFORE the match, which a marked
@@ -49,9 +70,50 @@
49
70
  import { escapeHtml } from "@mulmoclaude/common";
50
71
  const ASCII_ALNUM = /[A-Za-z0-9]/;
51
72
  const ASCII_DIGIT = /\d/;
52
- /** Digits, separators and currency-ish punctuation only — `$1,000$`
53
- * is a price range, not an equation. */
54
- const NUMERIC_ONLY = /^[\s\d.,:;%+-]*$/;
73
+ const DIGITS = /^\d+$/;
74
+ /** Every character that separates the digit runs of a written number,
75
+ * in any locale: `1,000.50`, `1.000,50`, `1 000,50`. Which one groups
76
+ * and which one marks the decimal is exactly what cannot be known, so
77
+ * none of them is read as one or the other. */
78
+ const NUMBER_SEPARATORS = /[.,\s]/;
79
+ const THOUSAND = 3;
80
+ /** A bare number written the way MONEY is written rather than the way a
81
+ * quantity is — `1,000`, `1.000`, `1 000`, `1.000,50`, `12,345,678`.
82
+ *
83
+ * Read from the SHAPE, not from which separator appeared: a comma
84
+ * groups thousands in English and marks the decimal in most of Europe,
85
+ * and a dot does the opposite, so a rule that names one of them fails
86
+ * half the world's authors either way (codex, #2985).
87
+ *
88
+ * Two separators or more is money — a quantity does not need them. One
89
+ * separator followed by exactly three digits is ambiguous, `$1,500$` as
90
+ * much as `$1.500$`, and is read as money: that is the reading rule 5
91
+ * already had, and the one this file is narrowing rather than
92
+ * reversing. Everything else — `10000`, `1`, `1,5`, `3.14159` — is a
93
+ * number, and a number in a maths article is maths.
94
+ *
95
+ * A leading zero is NOT an exception, though it looks like one: a
96
+ * three-decimal sub-unit price is how fuel is priced (`$0.100` a litre),
97
+ * so `$0.100$` is as ambiguous as `$1.500$` and is read the same way
98
+ * (codex, #2985). Three digits after the separator is the whole test. */
99
+ function isMoneyShaped(body) {
100
+ const runs = body.split(NUMBER_SEPARATORS);
101
+ // Anything that is not digits-and-separators is not a written number at
102
+ // all — `x=1` and `\pi` land here and are maths by this rule.
103
+ if (!runs.every((run) => DIGITS.test(run)))
104
+ return false;
105
+ if (runs.length === 1)
106
+ return false;
107
+ if (runs.length > 2)
108
+ return true;
109
+ const [, tail] = runs;
110
+ if (tail === undefined)
111
+ return false;
112
+ return tail.length === THOUSAND;
113
+ }
114
+ /** The same set with the digits removed: a body of punctuation has
115
+ * nothing to typeset. */
116
+ const SEPARATORS_ONLY = /^[\s.,:;%+-]*$/;
55
117
  /** Index of the first `$` in `src` that could legally open math, or
56
118
  * `undefined` when there is none. Marked uses this to cut the
57
119
  * preceding text token, so returning a position is what gets the
@@ -86,8 +148,11 @@ export function isPlausibleInlineMath(body, after) {
86
148
  // Rule 4: `$5-$10`.
87
149
  if (ASCII_DIGIT.test(after))
88
150
  return false;
89
- // Rule 5.
90
- if (NUMERIC_ONLY.test(body))
151
+ // Rule 5. The shape of money is what says "price"; a plain number is
152
+ // just a number, and a number in a maths article is maths.
153
+ if (isMoneyShaped(body))
154
+ return false;
155
+ if (SEPARATORS_ONLY.test(body))
91
156
  return false;
92
157
  return true;
93
158
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mulmoclaude/markdown-utils",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "Browser-safe markdown / image rendering utilities shared by the MulmoClaude host and the markdown plugin",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",