Skip to content

Commit

Permalink
[micromark-extension-sub-super] Allow inline constructs inside
Browse files Browse the repository at this point in the history
  • Loading branch information
StaloneLab committed Mar 1, 2025
1 parent 93507c9 commit 1a0f5fc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
6 changes: 3 additions & 3 deletions packages/micromark-extension-sub-super/__tests__/spec.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ const specificationTests = {
'escaped - super': ['a\\^no\\^', '<p>a^no^</p>'],
'escaped inside': ['a^\\^^', '<p>a<sup>^</sup></p>', true],
'lone tilde': ['a ~ b', '<p>a ~ b</p>'],
'can contain inline - super': ['my ^*important*^ superscript', '<p>my <sup><em>important</em></sup> superscript</p>', true],
'can contain inline - sub': ['my ~*important*~ subscript', '<p>my <sub><em>important</em></sub> subscript</p>', true],
'can contain inline - super': ['my ^*important*^ superscript', '<p>my <sup><em>important</em></sup> superscript</p>'],
'can contain inline - sub': ['my ~*important*~ subscript', '<p>my <sub><em>important</em></sub> subscript</p>'],
'can be contained': ['my *im~por~tant* subscript', '<p>my <em>im<sub>por</sub>tant</em> subscript</p>'],
'can be self-contained': ['2^2^2^^ = 16', '<p>2<sup>2<sup>2</sup></sup> = 16</p>', true],
'can be cross-contained': ['remark-~sub-^super^~', '<p>remark-<sub>sub-<sup>super</sup></sub></p>', true]
'can be cross-contained': ['remark-~sub-^super^~', '<p>remark-<sub>sub-<sup>super</sup></sub></p>']
}

const renderString = (fixture) =>
Expand Down
41 changes: 20 additions & 21 deletions packages/micromark-extension-sub-super/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,22 @@ export default function micromarkSubSuper (options = {}) {
}

function tokenizeFactory (subCharCode, superCharCode) {
const CONSTRUCT_SUB_NAME = 'subString'
const CONSTRUCT_SUPER_NAME = 'superString'

return tokenizeSubSuper

function tokenizeSubSuper (effects, ok, nok) {
let constructSequenceName
return start

function isMatchingConstruct (code) {
return (
(constructSequenceName !== CONSTRUCT_SUB_NAME && code === superCharCode) ||
(constructSequenceName !== CONSTRUCT_SUPER_NAME && code === subCharCode)
)
}

function start (code) {
// We should not have entered here at all
if (code !== subCharCode && code !== superCharCode) return nok(code)
Expand All @@ -35,44 +46,32 @@ function tokenizeFactory (subCharCode, superCharCode) {
effects.consume(code)
effects.exit('subSuperSequence')

if (code === subCharCode) effects.enter('subString')
else if (code === superCharCode) effects.enter('superString')
effects.enter('data')
if (code === subCharCode) constructSequenceName = CONSTRUCT_SUB_NAME
else if (code === superCharCode) constructSequenceName = CONSTRUCT_SUPER_NAME

effects.enter(constructSequenceName)
effects.enter('chunkText', { contentType: 'text' })

Check failure on line 53 in packages/micromark-extension-sub-super/lib/index.js

View workflow job for this annotation

GitHub Actions / Check linting problems

Irregular whitespace not allowed

return afterStart
}

function afterStart (code) {
if (code === subCharCode ||
code === superCharCode ||
code === codes.space) return nok(code)
if (isMatchingConstruct(code) || code === codes.space) return nok(code)

return content(code)
}

function content (code) {
if (code === subCharCode) return subEnd(code)
else if (code === superCharCode) return superEnd(code)

if (isMatchingConstruct(code)) return end(code)
if (code === codes.eof || markdownLineEnding(code)) return nok(code)

effects.consume(code)
return content
}

function subEnd (code) {
effects.exit('data')
effects.exit('subString')
return end(code)
}

function superEnd (code) {
effects.exit('data')
effects.exit('superString')
return end(code)
}

function end (code) {
effects.exit('chunkText')
effects.exit(constructSequenceName)
effects.enter('subSuperSequence')
effects.consume(code)
effects.exit('subSuperSequence')
Expand Down

0 comments on commit 1a0f5fc

Please sign in to comment.