MCPcopy Create free account
hub / github.com/UNLINEARITY/Obsidian-CodeSpace / expandCodeEmbedsInMarkdown

Function expandCodeEmbedsInMarkdown

src/code_embed_markdown.ts:234–286  ·  view source on GitHub ↗
(
	plugin: CodeSpacePlugin,
	markdown: string,
	sourceFile: TFile
)

Source from the content-addressed store, hash-verified

232}
233
234export async function expandCodeEmbedsInMarkdown(
235 plugin: CodeSpacePlugin,
236 markdown: string,
237 sourceFile: TFile
238): Promise<string> {
239 const allowedExtensions = collectAllowedExtensions(plugin);
240 const lines = markdown.split("\n");
241 const output: string[] = [];
242 let activeFence: { marker: "`" | "~"; length: number } | null = null;
243
244 for (const line of lines) {
245 const fenceMatch = line.match(/^(\s*)(`{3,}|~{3,})/);
246 if (fenceMatch?.[2]) {
247 const marker = fenceMatch[2][0] as "`" | "~";
248 const length = fenceMatch[2].length;
249
250 if (!activeFence) {
251 activeFence = { marker, length };
252 } else if (activeFence.marker === marker && length >= activeFence.length) {
253 activeFence = null;
254 }
255
256 output.push(line);
257 continue;
258 }
259
260 if (activeFence) {
261 output.push(line);
262 continue;
263 }
264
265 const embedMatch = line.match(/^([>\s]*)!\[\[([^\]]+)\]\]\s*$/);
266 if (!embedMatch?.[2]) {
267 output.push(line);
268 continue;
269 }
270
271 const indent = embedMatch[1] ?? "";
272 const rawReference = embedMatch[2].trim();
273 const resolved = resolveCodeEmbedReference(plugin, rawReference, sourceFile.path, allowedExtensions);
274 if (!resolved) {
275 output.push(line);
276 continue;
277 }
278
279 const fullContent = await plugin.app.vault.read(resolved.file);
280 const slicedContent = sliceFileContent(fullContent, resolved.startLine, resolved.endLine);
281 const fencedCode = createFencedCodeBlock(slicedContent, resolved.file.extension.toLowerCase());
282 output.push(indentMultiline(fencedCode, indent));
283 }
284
285 return output.join("\n");
286}

Callers 1

buildExpandedMarkdownFunction · 0.90

Calls 5

collectAllowedExtensionsFunction · 0.85
sliceFileContentFunction · 0.85
createFencedCodeBlockFunction · 0.85
indentMultilineFunction · 0.85

Tested by

no test coverage detected