Skip to content

Commit b5ab8f3

Browse files
committed
fix(common) Add logic to handle more multilines and nested blockquote content
Signed-off-by: Jerome Simeon <[email protected]>
1 parent 11e6c5e commit b5ab8f3

File tree

4 files changed

+27
-12
lines changed

4 files changed

+27
-12
lines changed

packages/markdown-common/src/CommonMarkTransformer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class CommonMarkTransformer {
102102
parameters.result = '';
103103
parameters.first = true;
104104
parameters.indent = 0;
105+
parameters.blockIndent = 0;
105106
const visitor = new ToMarkdownStringVisitor(this.options);
106107
input.accept(visitor, parameters);
107108
return parameters.result.trim();
@@ -120,6 +121,7 @@ class CommonMarkTransformer {
120121
parameters.result = '';
121122
parameters.first = true;
122123
parameters.indent = 0;
124+
parameters.blockIndent = 0;
123125
const visitor = new ToMarkdownStringVisitor(this.options);
124126
const result = ToMarkdownStringVisitor.visitChildren(visitor,input,parameters);
125127
// console.log('RESULT!' + result.trim());

packages/markdown-common/src/ToMarkdownStringVisitor.js

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ class ToMarkdownStringVisitor {
4444
static visitChildren(visitor, thing, parameters) {
4545
if(!parameters) {
4646
parameters = {};
47-
parameters.result = '';
48-
parameters.first = false;
4947
parameters.indent = 0;
48+
parameters.first = false;
49+
parameters.blockIndent = 0;
5050
}
51+
parameters.result = '';
5152

5253
if(thing.nodes) {
5354
thing.nodes.forEach(node => {
@@ -63,11 +64,12 @@ class ToMarkdownStringVisitor {
6364
* @param {*} parametersOut - the current parameters
6465
* @return {*} the new parameters with first set to true
6566
*/
66-
static mkParametersIn(parametersOut) {
67+
static mkParametersInBlock(parametersOut) {
6768
let parameters = {};
6869
parameters.result = '';
69-
parameters.first = true;
70+
parameters.first = false;
7071
parameters.indent = parametersOut.indent; // Same indentation
72+
parameters.blockIndent = parametersOut.blockIndent+1;
7173
return parameters;
7274
}
7375

@@ -81,6 +83,7 @@ class ToMarkdownStringVisitor {
8183
parameters.result = '';
8284
parameters.first = true;
8385
parameters.indent = parametersOut.indent+1; // Increases indentation
86+
parameters.blockIndent = parametersOut.blockIndent;
8487
return parameters;
8588
}
8689

@@ -93,6 +96,15 @@ class ToMarkdownStringVisitor {
9396
return new Array(parameters.indent*3+1).join(' ');
9497
}
9598

99+
/**
100+
* Create blockQuote indendation
101+
* @param {*} parameters - the current parameters
102+
* @return {string} prefix for blockquote indentation
103+
*/
104+
static mkIndentBlock(parameters) {
105+
return '> '.repeat(parameters.blockIndent);
106+
}
107+
96108
/**
97109
* Create Setext heading
98110
* @param {number} level - the heading level
@@ -121,8 +133,10 @@ class ToMarkdownStringVisitor {
121133
* @param {*} level - number of new lines
122134
*/
123135
static newBlock(parameters,level) {
124-
const newlines = parameters.first ? '' : Array(level).fill('\n').join('');
136+
const fixLevel = parameters.blockIndent === 0 ? level : 1;
137+
const newlines = parameters.first ? '' : Array(fixLevel).fill('\n').join('');
125138
parameters.result += newlines;
139+
parameters.result += ToMarkdownStringVisitor.mkIndentBlock(parameters);
126140
}
127141

128142
/**
@@ -159,9 +173,8 @@ class ToMarkdownStringVisitor {
159173
parameters.result += `**${ToMarkdownStringVisitor.visitChildren(this, thing)}**`;
160174
break;
161175
case 'BlockQuote': {
162-
const parametersIn = ToMarkdownStringVisitor.mkParametersIn(parameters);
163-
ToMarkdownStringVisitor.newBlock(parameters,2);
164-
parameters.result += `> ${ToMarkdownStringVisitor.visitChildren(this, thing, parametersIn)}`;
176+
const parametersIn = ToMarkdownStringVisitor.mkParametersInBlock(parameters);
177+
parameters.result += `${ToMarkdownStringVisitor.visitChildren(this, thing, parametersIn)}`;
165178
}
166179
break;
167180
case 'Heading': {
@@ -182,7 +195,7 @@ class ToMarkdownStringVisitor {
182195
parameters.result += '\\\n';
183196
break;
184197
case 'Softbreak':
185-
parameters.result += '\n';
198+
parameters.result += '\n' + ToMarkdownStringVisitor.mkIndentBlock(parameters);
186199
break;
187200
case 'Link':
188201
parameters.result += `[${ToMarkdownStringVisitor.visitChildren(this, thing)}](${thing.destination} "${thing.title ? thing.title : ''}")`;
@@ -192,7 +205,7 @@ class ToMarkdownStringVisitor {
192205
break;
193206
case 'Paragraph':
194207
ToMarkdownStringVisitor.newBlock(parameters,2);
195-
parameters.result += `${ToMarkdownStringVisitor.visitChildren(this, thing)}`;
208+
parameters.result += `${ToMarkdownStringVisitor.visitChildren(this, thing, parameters)}`;
196209
break;
197210
case 'HtmlBlock':
198211
ToMarkdownStringVisitor.newBlock(parameters,2);

packages/markdown-docx/src/DocxTransformer.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe.only('import docx', () => {
4848
getDocxFiles().forEach(([file, docx], i) => {
4949
it(`converts ${file} to ciceromark`, async () => {
5050
const json = await docxTransformer.toCiceroMark(docx, 'json');
51-
console.log(JSON.stringify(json, null, 4));
51+
//console.log(JSON.stringify(json, null, 4));
5252
expect(json).toMatchSnapshot(); // (1)
5353
});
5454
});

packages/markdown-pdf/src/PdfTransformer.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe.only('pdf import', () => {
4848
getPdfFiles().forEach(([file, pdfContent], i) => {
4949
it(`converts ${file} to cicero mark`, async () => {
5050
const ciceroMarkDom = await pdfTransformer.toCiceroMark(pdfContent, 'json');
51-
console.log(JSON.stringify(ciceroMarkDom, null, 4));
51+
//console.log(JSON.stringify(ciceroMarkDom, null, 4));
5252
expect(ciceroMarkDom).toMatchSnapshot(); // (1)
5353
});
5454
});

0 commit comments

Comments
 (0)