Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ The following chemical formulas should be subscripted automatically:
* CO2
* CF4
* CH4
* H2
* H2O
* N2O
* NF3
* O2
* O3
* SF6

Those formulas should not be subscripted when they appear in URLs, for example [CO2](https://www.climateinteractive.org/CO2).
Those formulas should not be subscripted when they appear in URLs, for example [CO2](https://www.climateinteractive.org/CO2).
2 changes: 2 additions & 0 deletions packages/docs-builder/src/gen-html.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,11 @@ To fix, ensure there are no spaces between link text and link url/reference, for

describe('subscriptify', () => {
it('should convert chemical formulas', () => {
expect(subscriptify('CO2')).toBe('CO<sub>2</sub>')
expect(subscriptify('This is -CO2-')).toBe('This is -CO<sub>2</sub>-')
expect(subscriptify('This is -CF4-')).toBe('This is -CF<sub>4</sub>-')
expect(subscriptify('This is -CH4-')).toBe('This is -CH<sub>4</sub>-')
expect(subscriptify('This is -H2-')).toBe('This is -H<sub>2</sub>-')
expect(subscriptify('This is -H2O-')).toBe('This is -H<sub>2</sub>O-')
expect(subscriptify('This is -N2O-')).toBe('This is -N<sub>2</sub>O-')
expect(subscriptify('This is -NF3-')).toBe('This is -NF<sub>3</sub>-')
Expand Down
11 changes: 8 additions & 3 deletions packages/docs-builder/src/gen-html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ const subscriptMap = new Map([
['CO2', 'CO<sub>2</sub>'],
['CF4', 'CF<sub>4</sub>'],
['CH4', 'CH<sub>4</sub>'],
['H2O', 'H<sub>2</sub>O'],
['H2', 'H<sub>2</sub>'],
['N2O', 'N<sub>2</sub>O'],
['NF3', 'NF<sub>3</sub>'],
['O2', 'O<sub>2</sub>'],
Expand Down Expand Up @@ -607,19 +607,24 @@ export function convertMarkdownToHtml(context: Context, md: string): string {
* CO2
* CF4
* CH4
* H2
* H2O
* N2O
* NF3
* O2
* O3
* SF6
*
* Note that we only convert in the case where the chemical name is either at the
* beginning of a line or not preceded by a letter or digit, so that we convert
* things like "H2" or "non-H2" but not "CH3CH2CH3".
*
* @param s The input string.
* @return A new string containing subscripted chemical names.
*/
export function subscriptify(s: string): string {
return s.replace(/(CO2|CF4|CH4|H2O|N2O|NF3|O2|O3|SF6)/g, (_m, m1) => {
return subscriptMap.get(m1)
return s.replace(/(^|[^a-zA-Z0-9])(CO2|CF4|CH4|H2|N2O|NF3|O2|O3|SF6)/g, (_m, m1, m2) => {
return `${m1 || ''}${subscriptMap.get(m2)}`
})
}

Expand Down