Skip to content

Commit

Permalink
fix listing syntax highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
sransara committed Jul 5, 2024
1 parent 66ddb35 commit 176006a
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 27 deletions.
40 changes: 40 additions & 0 deletions adocx/templates/listing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Reference:
// - https://github.com/asciidoctor/asciidoctor-backends/blob/master/erb/html5/block_math.html.erb

import type { Block } from 'asciidoctor';
import { type Template } from 'astro-adocx/types.ts';
import { addOnceToAstroFence } from 'astro-adocx/utils/astroFence';
import { atag } from 'astro-adocx/utils/asx.ts';

export const convert: Template<Block>['convert'] = (node: Block, opts?: any) => {
const id = node.getId();
const title = node.getCaptionedTitle();
const roles = node.getRoles().join(' ');
const content = node.getContent();
const lang = node.getAttribute('language');

addOnceToAstroFence(
node,
"import CodeListing from '@/src/lib/astro/codeListing/CodeListing.astro';",
);
return atag('div', {
id,
class: `listingblock ${roles}`,
children: [
title &&
atag('div', {
class: 'title',
children: [title],
}),
atag('div', {
class: 'content',
children: [
atag('CodeListing', {
lang,
children: [content],
}),
],
}),
],
});
};
25 changes: 25 additions & 0 deletions src/lib/astro/codeListing/CodeListing.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
import { codeToHtml, bundledLanguages } from 'shiki';
import { decodeSpecialChars } from 'astro-adocx/utils/string.ts';
interface Props {
lang: string;
}
const { lang } = Astro.props;
let content: string | undefined = await Astro.slots.render('default');
content = (content || '').replace(
/<i class="conum" data-value="\d+"><\/i><b>\((\d+)\)<\/b>/g,
'\u0D82$1\u0D83',
);
content = decodeSpecialChars(content);
let output = await codeToHtml(content, {
lang: lang in bundledLanguages ? lang : 'text',
theme: 'github-light',
});
output = output.replace(/\u0D82(\d+)\u0D83/g, '<i class="conum" data-value="$1"></i><b>$1</b>');
---

<Fragment set:html={output} />
27 changes: 0 additions & 27 deletions src/lib/astro/shiki/Shiki.astro

This file was deleted.

0 comments on commit 176006a

Please sign in to comment.