Skip to content

dc.Markdown does not distinguished between broken and healthy links #174

Description

@btonasse

The default Obsidian link renderer will add the is-unresolved class to any broken links, but dc.Markdown will not handle this at all.

My workaround is to post-process it:

const Markdown = ({
  content,
  sourcePath,
}: {
  content: string;
  sourcePath?: string;
}) => {
  const wrapperRef = dc.useRef<HTMLDivElement>(null);
  const currentPath = dc.useCurrentPath();

  const path = sourcePath || currentPath;

  dc.useEffect(() => {
    const root = wrapperRef.current;
    if (!root) return;

    // Wait until dc.Markdown has rendered and check if links are broken
    queueMicrotask(() => {
      root.querySelectorAll("a.internal-link").forEach((link) => {
        const linkpath =
          link.getAttribute("data-href") ?? link.getAttribute("href");

        if (!linkpath) return;

        const resolved = dc.app.metadataCache.getFirstLinkpathDest(
          linkpath,
          path,
        );

        link.classList.toggle("is-unresolved", !resolved);
      });
    });
  }, [dc.app, content, path]);

  return (
    <div ref={wrapperRef}>
      <dc.Markdown content={content} sourcePath={path} />
    </div>
  );
};

It would be nice if this was incorporated into the original component's effect

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions