diff --git a/packages/core/src/lib/markdown-it/plugins/markdown-it-center-text.ts b/packages/core/src/lib/markdown-it/plugins/markdown-it-center-text.ts index 23e5d6ba14..3df8a619f1 100644 --- a/packages/core/src/lib/markdown-it/plugins/markdown-it-center-text.ts +++ b/packages/core/src/lib/markdown-it/plugins/markdown-it-center-text.ts @@ -28,6 +28,11 @@ import MarkdownIt from 'markdown-it'; import StateInline from 'markdown-it/lib/rules_inline/state_inline'; +// Delimiter markers are non-negative character codes, so negative sentinels +// used not to collide with markers used by other inline rules. +const CENTERTEXT_OPEN_MARKER = -1; +const CENTERTEXT_CLOSE_MARKER = -2; + /** * A markdown-it plugin to center text using the syntax ->text<- */ @@ -48,7 +53,7 @@ export function centertext_plugin(md: MarkdownIt): void { token = state.push('text', '', 0); token.content = '->'; state.delimiters.push({ - marker: 45, // CHANGED: Use character code 45 instead of string '->' + marker: CENTERTEXT_OPEN_MARKER, jump: 0, token: state.tokens.length - 1, length: 2, @@ -64,7 +69,7 @@ export function centertext_plugin(md: MarkdownIt): void { token = state.push('text', '', 0); token.content = '<-'; state.delimiters.push({ - marker: 60, // CHANGED: Use character code 60 instead of string '<-' + marker: CENTERTEXT_CLOSE_MARKER, jump: 0, token: state.tokens.length - 1, length: 2, @@ -94,9 +99,9 @@ export function centertext_plugin(md: MarkdownIt): void { for (i = 0; i < max; i++) { delim = delimiters[i]; - if (delim.marker === 45/* - */) { + if (delim.marker === CENTERTEXT_OPEN_MARKER) { foundStart = true; - } else if (delim.marker === 60/* < */) { + } else if (delim.marker === CENTERTEXT_CLOSE_MARKER) { foundEnd = true; } } @@ -104,7 +109,7 @@ export function centertext_plugin(md: MarkdownIt): void { for (i = 0; i < max; i++) { delim = delimiters[i]; - if (delim.marker === 45/* - */) { + if (delim.marker === CENTERTEXT_OPEN_MARKER) { foundStart = true; token = state.tokens[delim.token]; token.type = 'centertext_open'; @@ -113,7 +118,7 @@ export function centertext_plugin(md: MarkdownIt): void { token.markup = '->'; token.content = ''; token.attrs = [['class', 'text-center']]; - } else if (delim.marker === 60/* < */) { + } else if (delim.marker === CENTERTEXT_CLOSE_MARKER) { if (foundStart) { token = state.tokens[delim.token]; token.type = 'centertext_close'; diff --git a/packages/core/test/unit/lib/markdown-it/plugins/markdown-it-center-text.test.ts b/packages/core/test/unit/lib/markdown-it/plugins/markdown-it-center-text.test.ts index cb1373c453..b5e2cc2290 100644 --- a/packages/core/test/unit/lib/markdown-it/plugins/markdown-it-center-text.test.ts +++ b/packages/core/test/unit/lib/markdown-it/plugins/markdown-it-center-text.test.ts @@ -1,6 +1,9 @@ import markdownIt from 'markdown-it'; import { centertext_plugin } from '../../../../../src/lib/markdown-it/plugins/markdown-it-center-text.js'; +import { + createDoubleDelimiterInlineRule, +} from '../../../../../src/lib/markdown-it/plugins/markdown-it-double-delimiter.js'; describe('markdown-it-center-text plugin', () => { let md: markdownIt; @@ -72,4 +75,13 @@ describe('markdown-it-center-text plugin', () => { // which is possibly not schematicallly correct expect(result).toContain('reversed'); }); + + test('should not collide with the "--" small-text rule sharing the same "-" character', () => { + const mdWithSmall = markdownIt(); + mdWithSmall.use(createDoubleDelimiterInlineRule('--', 'small', 'emphasis')); + mdWithSmall.use(centertext_plugin); + + expect(mdWithSmall.renderInline('--Small--, ->Center-align<-')) + .toBe('Small,
Center-align
'); + }); });