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 numberDiff line numberDiff line change
Expand Up@@ -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<-
*/
Expand All@@ -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,
Expand All@@ -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,
Expand DownExpand Up@@ -94,17 +99,17 @@ 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;
}
}
if (foundStart && foundEnd) {
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';
Expand All@@ -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';
Expand Down
Original file line numberDiff line numberDiff line change
@@ -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;
Expand DownExpand Up@@ -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('<span class="small">Small</span>, <div class="text-center">Center-align</div>');
});
});
Loading