Skip to content

Commit

Permalink
Add code blocks support
Browse files Browse the repository at this point in the history
Resolves: #412
  • Loading branch information
ggodlewski committed May 16, 2024
1 parent 23da834 commit 1d9bce7
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/odt/postprocess/convertCodeBlockParagraphs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {MarkdownNode, MarkdownNodes} from '../MarkdownNodes.ts';
import {walkRecursiveSync} from '../markdownNodesUtils.ts';

const CODEBLOCK_START = '';
const CODEBLOCK_END = '';

function isCodeBlockPara(chunk: MarkdownNode, type: string) {
if (chunk.isTag === true && ['P'].includes(chunk.tag)) {
if (chunk.children.length !== 1) {
return false;
}

const firstChunk = chunk.children[0];

if ('text' in firstChunk) {
const txt = firstChunk.text;
return (txt === type);
}

}
return false;
}

export function convertCodeBlockParagraphs(markdownChunks: MarkdownNodes) {
walkRecursiveSync(markdownChunks.body, async (node, ctx: { nodeIdx: number }) => {
if (isCodeBlockPara(node, CODEBLOCK_START)) {
for (let nodeIdx2 = ctx.nodeIdx + 1; nodeIdx2 < node.parent.children.length; nodeIdx2++) {
const node2 = node.parent.children[nodeIdx2];
if (isCodeBlockPara(node2, CODEBLOCK_END)) {
console.log('hhh', ctx.nodeIdx, nodeIdx2);
const inner = node.parent.children.splice(ctx.nodeIdx + 1, nodeIdx2 - (ctx.nodeIdx + 1));

const toInsert = inner.map((part, idx) => {
if (!part.isTag) {
const pre = markdownChunks.createNode('PRE');
pre.children.splice(0, 0, ...inner);
pre.payload.lang = (idx < inner.length - 1) ? 'codeblock' : 'codeblockend';
return pre;
}
if (part.isTag && part.tag === 'P') {
part.tag = 'PRE';
part.payload.lang = (idx < inner.length - 1) ? 'codeblock' : 'codeblockend';
return part;
}
return part;
});

const emptyLine = markdownChunks.createNode('EMPTY_LINE/');
emptyLine.comment = 'addEmptyLines.ts: after codeblock';
toInsert.push(emptyLine);
node.parent.children.splice(ctx.nodeIdx, 2, ...toInsert);
break;
}
}
}
});
}
4 changes: 4 additions & 0 deletions src/odt/postprocess/mergeParagraphs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export function mergeParagraphs(markdownChunks: MarkdownNodes) {
if (chunk.tag === 'PRE' && chunk.payload?.lang === 'math') {
return;
}
if (chunk.tag === 'PRE' && chunk.payload?.lang === 'codeblockend') {
chunk.payload.lang = '';
return;
}

const nextChunk = chunk.parent.children[ctx.nodeIdx + 1];
if (nextChunk?.isTag && nextChunk.tag === chunk.tag) {
Expand Down
2 changes: 2 additions & 0 deletions src/odt/postprocess/postProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {convertMathMl} from './convertMathMl.ts';
import {unwrapEmptyPre} from './unwrapEmptyPre.ts';
import {convertGoogleUrls} from './convertGoogleUrls.ts';
import {fixIdLinks} from './fixIdLinks.ts';
import {convertCodeBlockParagraphs} from './convertCodeBlockParagraphs.ts';

export async function postProcess(chunks: MarkdownNodes, rewriteRules: RewriteRule[]) {
convertToc(chunks);
Expand All @@ -35,6 +36,7 @@ export async function postProcess(chunks: MarkdownNodes, rewriteRules: RewriteRu
fixSpacesInsideInlineFormatting(chunks);
await fixBoldItalic(chunks);
hideSuggestedChanges(chunks);
convertCodeBlockParagraphs(chunks);
convertMathMl(chunks);

trimParagraphs(chunks);
Expand Down
6 changes: 6 additions & 0 deletions test/odt_md/MarkDownTransform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ describe('MarkDownTransformTest', () => {
assert.ok(compareTexts(testMarkdown, markdown, false));
});

it('test ./code-blocks.md', async () => {
const testMarkdown = fs.readFileSync(__dirname + '/code-blocks.md').toString();
const markdown = await transformOdt('code-blocks');
assert.ok(compareTexts(testMarkdown, markdown, false));
});

});

async function transformOdt(id: string) {
Expand Down
30 changes: 30 additions & 0 deletions test/odt_md/code-blocks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
**This** is a line

**This** is line two

* This is a bulleted test
* Second line

```
void hi() {
}
hi();
```

```
function hi2() {
}
hi2();
```

This is a cool new wikiGDrive! With a change.

![](100000000000080100000600B855EF66465B18B1.jpg)

![](1000000000000801000006008E6541DADAF62125.jpg)

![](10000000000009C400000753DA4A57339A651C1F.jpg)

![](10000000000009C40000075322E66BBDAA567D45.jpg)
Binary file added test/odt_md/code-blocks.odt
Binary file not shown.

0 comments on commit 1d9bce7

Please sign in to comment.