Skip to content

Commit 64e0a40

Browse files
committed
Skip Shiki for plain-text posts
Only initialize Shiki when the post body actually contains a code block. This keeps the lazy-loaded syntax highlighter from adding a multi-second delay to the first plain-text status creation request, while preserving highlighted code blocks when they are needed. #435 Assisted-by: Codex:gpt-5.4
1 parent 23e5b7c commit 64e0a40

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

src/text.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ interface Env {
3636

3737
type MarkdownItPlugin = Parameters<MarkdownIt["use"]>[0];
3838

39+
const FENCED_CODE_BLOCK_REGEXP = /(^|\n) {0,3}(?:`{3,}|~{3,})/;
40+
const INDENTED_CODE_BLOCK_REGEXP = /(^|\n)(?: {4}|\t)\S/;
41+
3942
let shikiPluginPromise: Promise<MarkdownItPlugin> | undefined;
4043

4144
async function getShikiPlugin(): Promise<MarkdownItPlugin> {
@@ -55,6 +58,12 @@ async function getShikiPlugin(): Promise<MarkdownItPlugin> {
5558
return await shikiPluginPromise;
5659
}
5760

61+
function containsCodeBlock(text: string): boolean {
62+
return (
63+
FENCED_CODE_BLOCK_REGEXP.test(text) || INDENTED_CODE_BLOCK_REGEXP.test(text)
64+
);
65+
}
66+
5867
export async function formatText(
5968
db: PgDatabase<
6069
PostgresJsQueryResultHKT,
@@ -68,8 +77,6 @@ export async function formatText(
6877
documentLoader?: DocumentLoader;
6978
},
7079
): Promise<FormatResult> {
71-
const shiki = await getShikiPlugin();
72-
7380
// List all mentions:
7481
const draft = new MarkdownIt({ linkify: true, html: ALLOW_HTML })
7582
.use(mention, {})
@@ -167,8 +174,10 @@ export async function formatText(
167174
}
168175
return new URL(link, new URL("/", options.url)).href;
169176
},
170-
})
171-
.use(shiki);
177+
});
178+
if (containsCodeBlock(text)) {
179+
md.use(await getShikiPlugin());
180+
}
172181
const env: Env = {
173182
hashtags: [],
174183
previewLink: null,

0 commit comments

Comments
 (0)