Skip to content

Commit

Permalink
support for scientific notation and decimal numbers
Browse files Browse the repository at this point in the history
in the `si` role
  • Loading branch information
jan-david-fischbach committed Sep 5, 2023
1 parent 27c7557 commit bf55487
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 19 additions & 0 deletions packages/myst-parser/tests/roles/si.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions packages/myst-roles/src/si.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,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);
Expand Down

0 comments on commit bf55487

Please sign in to comment.