diff --git a/examples/sample-docs/projects/sample-guide/content/appendix.md b/examples/sample-docs/projects/sample-guide/content/appendix.md
index 0b829db..96abe29 100644
--- a/examples/sample-docs/projects/sample-guide/content/appendix.md
+++ b/examples/sample-docs/projects/sample-guide/content/appendix.md
@@ -8,6 +8,7 @@ The following chemical formulas should be subscripted automatically:
* CO2
* CF4
* CH4
+* H2
* H2O
* N2O
* NF3
@@ -15,4 +16,4 @@ The following chemical formulas should be subscripted automatically:
* O3
* SF6
-Those formulas should not be subscripted when they appear in URLs, for example [CO2](https://www.climateinteractive.org/CO2).
\ No newline at end of file
+Those formulas should not be subscripted when they appear in URLs, for example [CO2](https://www.climateinteractive.org/CO2).
diff --git a/packages/docs-builder/src/gen-html.spec.ts b/packages/docs-builder/src/gen-html.spec.ts
index 208ce2a..db47d9a 100644
--- a/packages/docs-builder/src/gen-html.spec.ts
+++ b/packages/docs-builder/src/gen-html.spec.ts
@@ -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('CO2')
expect(subscriptify('This is -CO2-')).toBe('This is -CO2-')
expect(subscriptify('This is -CF4-')).toBe('This is -CF4-')
expect(subscriptify('This is -CH4-')).toBe('This is -CH4-')
+ expect(subscriptify('This is -H2-')).toBe('This is -H2-')
expect(subscriptify('This is -H2O-')).toBe('This is -H2O-')
expect(subscriptify('This is -N2O-')).toBe('This is -N2O-')
expect(subscriptify('This is -NF3-')).toBe('This is -NF3-')
diff --git a/packages/docs-builder/src/gen-html.ts b/packages/docs-builder/src/gen-html.ts
index d80d85d..a6099e1 100644
--- a/packages/docs-builder/src/gen-html.ts
+++ b/packages/docs-builder/src/gen-html.ts
@@ -528,7 +528,7 @@ const subscriptMap = new Map([
['CO2', 'CO2'],
['CF4', 'CF4'],
['CH4', 'CH4'],
- ['H2O', 'H2O'],
+ ['H2', 'H2'],
['N2O', 'N2O'],
['NF3', 'NF3'],
['O2', 'O2'],
@@ -607,6 +607,7 @@ export function convertMarkdownToHtml(context: Context, md: string): string {
* CO2
* CF4
* CH4
+ * H2
* H2O
* N2O
* NF3
@@ -614,12 +615,16 @@ export function convertMarkdownToHtml(context: Context, md: string): string {
* 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)}`
})
}