diff --git a/packages/myst-parser/tests/roles/si.yml b/packages/myst-parser/tests/roles/si.yml index 180eeb071..606123195 100644 --- a/packages/myst-parser/tests/roles/si.yml +++ b/packages/myst-parser/tests/roles/si.yml @@ -32,3 +32,22 @@ cases: unit: Hz units: [hertz] value: 100 Hz + + + - title: decimal number parses + markdown: '{si}`1.345e10 <\hertz>`' + mdast: + type: root + children: + - type: paragraph + children: + - type: mystRole + name: si + value: 1.345e10 <\hertz> + children: + - type: si + alt: hertz + number: '1.345e10' + unit: Hz + units: [hertz] + value: 1.345e10 Hz \ No newline at end of file diff --git a/packages/myst-roles/src/si.ts b/packages/myst-roles/src/si.ts index 44ae6d0bc..f15b35933 100644 --- a/packages/myst-roles/src/si.ts +++ b/packages/myst-roles/src/si.ts @@ -9,11 +9,12 @@ export const siRole: RoleSpec = { }, run(data: RoleData): GenericNode[] { const value = data.body as string; - const match = value.match(/([0-9]+)\s?<([\\a-zA-Z\s]+)>/); + const match = value.match(/(-?\d+(,\d+)*(\.\d+(e\d+)?)?)\s?<([\\a-zA-Z\s]+)>/); if (!match) { return [{ type: 'si', error: true, value }]; } - const [, number, commands] = match; + const number = match[1]; + const commands = match[match.length-1]; const parsed = [...commands.matchAll(/\\([a-zA-Z]+)/g)]; const units = parsed.filter((c) => !!c).map(([, c]) => c); const translated = units.map((c) => UNITS[c] ?? c);