From 628aba68371391a6c50aa5ad91a7830a96191b64 Mon Sep 17 00:00:00 2001 From: Jan David Fischbach <92856893+Jan-David-Black@users.noreply.github.com> Date: Tue, 5 Sep 2023 10:59:11 +0200 Subject: [PATCH] support for scientific notation and decimal numbers in the `si` role --- packages/myst-parser/tests/roles/si.yml | 19 +++++++++++++++++++ packages/myst-roles/src/si.ts | 5 +++-- 2 files changed, 22 insertions(+), 2 deletions(-) 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);