Skip to content

Commit b8158e8

Browse files
fix: include H2 in list of automatically subscripted chemicals (#96)
Fixes #95
1 parent 806e1cf commit b8158e8

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

examples/sample-docs/projects/sample-guide/content/appendix.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ The following chemical formulas should be subscripted automatically:
88
* CO2
99
* CF4
1010
* CH4
11+
* H2
1112
* H2O
1213
* N2O
1314
* NF3
1415
* O2
1516
* O3
1617
* SF6
1718

18-
Those formulas should not be subscripted when they appear in URLs, for example [CO2](https://www.climateinteractive.org/CO2).
19+
Those formulas should not be subscripted when they appear in URLs, for example [CO2](https://www.climateinteractive.org/CO2).

packages/docs-builder/src/gen-html.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,11 @@ To fix, ensure there are no spaces between link text and link url/reference, for
107107

108108
describe('subscriptify', () => {
109109
it('should convert chemical formulas', () => {
110+
expect(subscriptify('CO2')).toBe('CO<sub>2</sub>')
110111
expect(subscriptify('This is -CO2-')).toBe('This is -CO<sub>2</sub>-')
111112
expect(subscriptify('This is -CF4-')).toBe('This is -CF<sub>4</sub>-')
112113
expect(subscriptify('This is -CH4-')).toBe('This is -CH<sub>4</sub>-')
114+
expect(subscriptify('This is -H2-')).toBe('This is -H<sub>2</sub>-')
113115
expect(subscriptify('This is -H2O-')).toBe('This is -H<sub>2</sub>O-')
114116
expect(subscriptify('This is -N2O-')).toBe('This is -N<sub>2</sub>O-')
115117
expect(subscriptify('This is -NF3-')).toBe('This is -NF<sub>3</sub>-')

packages/docs-builder/src/gen-html.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ const subscriptMap = new Map([
528528
['CO2', 'CO<sub>2</sub>'],
529529
['CF4', 'CF<sub>4</sub>'],
530530
['CH4', 'CH<sub>4</sub>'],
531-
['H2O', 'H<sub>2</sub>O'],
531+
['H2', 'H<sub>2</sub>'],
532532
['N2O', 'N<sub>2</sub>O'],
533533
['NF3', 'NF<sub>3</sub>'],
534534
['O2', 'O<sub>2</sub>'],
@@ -607,19 +607,24 @@ export function convertMarkdownToHtml(context: Context, md: string): string {
607607
* CO2
608608
* CF4
609609
* CH4
610+
* H2
610611
* H2O
611612
* N2O
612613
* NF3
613614
* O2
614615
* O3
615616
* SF6
616617
*
618+
* Note that we only convert in the case where the chemical name is either at the
619+
* beginning of a line or not preceded by a letter or digit, so that we convert
620+
* things like "H2" or "non-H2" but not "CH3CH2CH3".
621+
*
617622
* @param s The input string.
618623
* @return A new string containing subscripted chemical names.
619624
*/
620625
export function subscriptify(s: string): string {
621-
return s.replace(/(CO2|CF4|CH4|H2O|N2O|NF3|O2|O3|SF6)/g, (_m, m1) => {
622-
return subscriptMap.get(m1)
626+
return s.replace(/(^|[^a-zA-Z0-9])(CO2|CF4|CH4|H2|N2O|NF3|O2|O3|SF6)/g, (_m, m1, m2) => {
627+
return `${m1 || ''}${subscriptMap.get(m2)}`
623628
})
624629
}
625630

0 commit comments

Comments
 (0)