From 43e8a4b2c49423da086f8ebd98de92935a595455 Mon Sep 17 00:00:00 2001 From: Guillaume Grossetie Date: Wed, 20 Sep 2023 18:55:08 +0200 Subject: [PATCH] Add an extra newline between code blocks (#28) --- src/index.js | 5 +++- test/converter.spec.js | 36 ++++++++++++++++++++++++++ test/fixtures/multiple-codeblocks.adoc | 32 +++++++++++++++++++++++ 3 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/multiple-codeblocks.adoc diff --git a/src/index.js b/src/index.js index 13888ac..e42bd0e 100644 --- a/src/index.js +++ b/src/index.js @@ -447,7 +447,10 @@ ${dd.getText()} if (result && result.length > 0) { if (!result.find(cell => cell.cell_type !== 'markdown')) { if (joinCharacter !== '' && result[0].metadata && result[0].metadata.node_name !== 'colist') { - lastCell.source[lastCell.source.length - 1] = lastCell.source[lastCell.source.length - 1] + joinCharacter + const blockJoiner = result[0].metadata.node_name === 'listing' && lastCell.metadata.node_name === 'listing' + ? '\n' + : '' + lastCell.source[lastCell.source.length - 1] = lastCell.source[lastCell.source.length - 1] + joinCharacter + blockJoiner } lastCell.source.push(...result.reduce((acc, cell) => acc.concat(cell.source), [])) // flatMap Node > 11 } else { diff --git a/test/converter.spec.js b/test/converter.spec.js index d088dbc..4b8eb74 100644 --- a/test/converter.spec.js +++ b/test/converter.spec.js @@ -407,4 +407,40 @@ matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(f"matrix={matrix}") `) }) + it('should convert code blocks as Markdown', () => { + const inputFile = path.join(__dirname, 'fixtures', 'multiple-codeblocks.adoc') + const result = asciidoctor.convertFile(inputFile, { + safe: 'safe', + backend: 'jupyter', + to_file: false + }) + expect(result).is.not.empty() + const ipynb = JSON.parse(result) + expect(ipynb.cells[0].source.join('')).is.equal(`\`\`\`cpp +#ifndef __MYFUNC_HPP__ #define __MYFUNC_HPP__ + +void mymsg(); + +#endif +\`\`\` + +\`\`\`cpp +#include + +void mymsg() +{ + std::cout << "Hello, world"; +} +\`\`\` + +\`\`\`cpp +#include "myfunc.hpp" + +int main() +{ +mymsg(); +return 0; +} +\`\`\``) + }) }) diff --git a/test/fixtures/multiple-codeblocks.adoc b/test/fixtures/multiple-codeblocks.adoc new file mode 100644 index 0000000..303e38b --- /dev/null +++ b/test/fixtures/multiple-codeblocks.adoc @@ -0,0 +1,32 @@ +.myfunc.hpp+ +[source,cpp] +---- +#ifndef __MYFUNC_HPP__ #define __MYFUNC_HPP__ + +void mymsg(); + +#endif +---- + +.myfunc.cpp +[source,cpp] +---- +#include + +void mymsg() +{ + std::cout << "Hello, world"; +} +---- + +.mymain.cpp +[source,cpp] +---- +#include "myfunc.hpp" + +int main() +{ +mymsg(); +return 0; +} +---- \ No newline at end of file