Skip to content

Commit

Permalink
Add an extra newline between code blocks (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie authored Sep 20, 2023
1 parent c8fba5c commit 43e8a4b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 36 additions & 0 deletions test/converter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <iostream>
void mymsg()
{
std::cout << "Hello, world";
}
\`\`\`
\`\`\`cpp
#include "myfunc.hpp"
int main()
{
mymsg();
return 0;
}
\`\`\``)
})
})
32 changes: 32 additions & 0 deletions test/fixtures/multiple-codeblocks.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.myfunc.hpp+
[source,cpp]
----
#ifndef __MYFUNC_HPP__ #define __MYFUNC_HPP__
void mymsg();
#endif
----

.myfunc.cpp
[source,cpp]
----
#include <iostream>
void mymsg()
{
std::cout << "Hello, world";
}
----

.mymain.cpp
[source,cpp]
----
#include "myfunc.hpp"
int main()
{
mymsg();
return 0;
}
----

0 comments on commit 43e8a4b

Please sign in to comment.