Commit 7746c56a authored by Enrique Alcántara's avatar Enrique Alcántara Committed by Andrew Fontaine

Configure if ordered list are incrementable in SSE

Allow to specify if ordered lists should have incremental
number when generating markdown in the Static Site Editor
parent 6b32bd5c
......@@ -4,6 +4,7 @@ import { defaults, repeat } from 'lodash';
const DEFAULTS = {
subListIndentSpaces: 4,
unorderedListBulletChar: '-',
incrementListMarker: false,
strong: '*',
emphasis: '_',
};
......@@ -15,12 +16,16 @@ const countIndentSpaces = text => {
};
const buildHTMLToMarkdownRender = (baseRenderer, formattingPreferences = {}) => {
const { subListIndentSpaces, unorderedListBulletChar, strong, emphasis } = defaults(
formattingPreferences,
DEFAULTS,
);
const {
subListIndentSpaces,
unorderedListBulletChar,
incrementListMarker,
strong,
emphasis,
} = defaults(formattingPreferences, DEFAULTS);
const sublistNode = 'LI OL, LI UL';
const unorderedListItemNode = 'UL LI';
const orderedListItemNode = 'OL LI';
const emphasisNode = 'EM, I';
const strongNode = 'STRONG, B';
......@@ -61,6 +66,11 @@ const buildHTMLToMarkdownRender = (baseRenderer, formattingPreferences = {}) =>
return baseResult.replace(/^(\s*)([*|-])/, `$1${unorderedListBulletChar}`);
},
[orderedListItemNode](node, subContent) {
const baseResult = baseRenderer.convert(node, subContent);
return incrementListMarker ? baseResult : baseResult.replace(/^(\s*)\d\./, '$11.');
},
[emphasisNode](node, subContent) {
const result = baseRenderer.convert(node, subContent);
......
---
title: When generating markdown for ordered lists, the list marker should not increment
merge_request: 36851
author:
type: changed
......@@ -68,6 +68,28 @@ describe('HTMLToMarkdownRenderer', () => {
);
});
describe('OL LI visitor', () => {
it.each`
listItem | result | incrementListMarker | action
${'2. list item'} | ${'1. list item'} | ${false} | ${'increments'}
${' 3. list item'} | ${' 1. list item'} | ${false} | ${'increments'}
${'3. list item'} | ${'3. list item'} | ${true} | ${'does not increment'}
`(
'$action a list item counter when incrementListMaker is $incrementListMarker',
({ listItem, result, incrementListMarker }) => {
const subContent = null;
htmlToMarkdownRenderer = buildHTMLToMarkdownRenderer(baseRenderer, {
incrementListMarker,
});
baseRenderer.convert.mockReturnValueOnce(listItem);
expect(htmlToMarkdownRenderer['OL LI'](NODE, subContent)).toBe(result);
expect(baseRenderer.convert).toHaveBeenCalledWith(NODE, subContent);
},
);
});
describe('STRONG, B visitor', () => {
it.each`
input | strongCharacter | result
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment