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

Function ensureCodeSpaceStylesInDocument

src/code_embed.ts:437–478  ·  view source on GitHub ↗
(targetDoc: Document, plugin: CodeSpacePlugin)

Source from the content-addressed store, hash-verified

435}
436
437function ensureCodeSpaceStylesInDocument(targetDoc: Document, plugin: CodeSpacePlugin) {
438 applyEmbedCssVariables(targetDoc, plugin);
439
440 // Some Obsidian popout windows do not automatically include plugin CSS. Copy the existing stylesheet
441 // reference from the main window so the embed UI renders consistently.
442 if (targetDoc.getElementById(CODE_SPACE_POPOUT_STYLE_ID)) return;
443
444 try {
445 const mainDoc = activeDocument;
446 const maybeLink1 = mainDoc.querySelector('link[href*="plugins/code-space/styles.css"]');
447 const maybeLink2 = mainDoc.querySelector('link[href*="/plugins/code-space/styles.css"]');
448 const link =
449 (maybeLink1 instanceof HTMLLinkElement ? maybeLink1 : null) ??
450 (maybeLink2 instanceof HTMLLinkElement ? maybeLink2 : null);
451
452 if (link?.href) {
453 const newLink = targetDoc.createElement("link");
454 newLink.id = CODE_SPACE_POPOUT_STYLE_ID;
455 newLink.rel = "stylesheet";
456 newLink.type = "text/css";
457 newLink.href = link.href;
458 targetDoc.head?.appendChild(newLink);
459 return;
460 }
461 } catch {
462 // Ignore.
463 }
464
465 // Fallback: clone the inline style tag if Obsidian injected plugin CSS as <style>.
466 const styleTags = Array.from(activeDocument.querySelectorAll("style"));
467 const codeSpaceStyle = styleTags.find((styleEl) => {
468 const text = styleEl.textContent ?? "";
469 return text.includes(".code-embed-container") || text.includes(".code-space-container");
470 });
471
472 if (codeSpaceStyle?.textContent) {
473 const newStyle = targetDoc.createElement("style");
474 newStyle.id = CODE_SPACE_POPOUT_STYLE_ID;
475 newStyle.textContent = codeSpaceStyle.textContent;
476 targetDoc.head?.appendChild(newStyle);
477 }
478}
479
480function resolveSourcePathForEmbed(embedEl: HTMLElement, plugin: CodeSpacePlugin): string {
481 const rememberedSourcePath = getRememberedSourcePath(embedEl);

Callers 3

queueCodeEmbedsInElementFunction · 0.85
refreshFunction · 0.85

Calls 1

applyEmbedCssVariablesFunction · 0.85

Tested by

no test coverage detected