Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

4.3 KB
Raw Blame
import {
  ASSET_EXTENSIONS,
  mediaKindForSidebarAssetExtension,
} from '@inkeep/open-knowledge-core/constants/upload';
import {
  buildRelativeMarkdownHref,
  type ClassifiedLinkTarget,
  classifyMarkdownHref,
  type DocLinkTarget,
} from '@inkeep/open-knowledge-core/utils/link-targets';
import { hashFromAssetPath, hashFromDocName, isSameHash } from '../lib/doc-hash';
import { openExternalUrl } from '../lib/external-link';
import { requestRepeatAnchorNavigation } from './anchor-navigation-event';
import { dispatchAssetClick } from './asset-dispatch';
import { isSafeNavigationUrl } from './safe-navigation-url';

export function getCurrentDocNameFromHash(locationHash = window.location.hash): string {
  const hashMatch = locationHash.match(/^#\/([^?#]+)/);
  return hashMatch ? decodeURIComponent(hashMatch[1]) : '';
}

export function classifyCurrentMarkdownHref(
  href: string,
  locationHash = window.location.hash,
): ClassifiedLinkTarget | null {
  return classifyMarkdownHref(href, getCurrentDocNameFromHash(locationHash));
}

export function toInternalHashHref({
  docName,
  anchor,
}: Pick<DocLinkTarget, 'docName' | 'anchor'>): string {
  return hashFromDocName(docName, anchor);
}

export function openHashHrefInNewTab(href: string): void {
  if (href.startsWith('#') || isSafeNavigationUrl(href)) {
    window.open(href, '_blank', 'noopener,noreferrer');
  } else {
    console.warn('[safe-nav] blocked non-safe scheme:', href);
  }
}

function navigateToInternalHashHref(resolved: Pick<DocLinkTarget, 'docName' | 'anchor'>): void {
  const href = toInternalHashHref(resolved);
  if (resolved.anchor && isSameHash(window.location.hash, href)) {
    requestRepeatAnchorNavigation();
    return;
  }
  window.location.assign(href);
}

export function openInternalHashHrefInNewTab(
  resolved: Pick<DocLinkTarget, 'docName' | 'anchor'>,
): void {
  openHashHrefInNewTab(toInternalHashHref(resolved));
}

function navigateToAssetPreview(assetPath: string): void {
  window.location.assign(hashFromAssetPath(assetPath));
}

interface ActivateAssetLinkParams {
  url: string;
  projectRelPath: string;
  ext: string;
  title: string;
  newTab: boolean;
}

interface ActivateAssetLinkDeps {
  navigate?: (assetPath: string) => void;
  dispatch?: typeof dispatchAssetClick;
}

export function activateAssetLink(
  { url, projectRelPath, ext, title, newTab }: ActivateAssetLinkParams,
  deps: ActivateAssetLinkDeps = {},
): void {
  const navigate = deps.navigate ?? navigateToAssetPreview;
  const dispatch = deps.dispatch ?? dispatchAssetClick;
  if (newTab) {
    if (mediaKindForSidebarAssetExtension(ext) === 'excalidraw') {
      openInternalHashHrefInNewTab({ docName: projectRelPath, anchor: null });
    } else {
      void dispatch({
        url: ASSET_EXTENSIONS.has(ext) ? url : hashFromAssetPath(projectRelPath),
        projectRelPath,
        ext,
        title,
        forceOsDelegation: true,
      });
    }
    return;
  }
  navigate(projectRelPath);
}

export function shouldOpenInNewTab(event: { metaKey: boolean; ctrlKey: boolean }): boolean {
  return event.metaKey || event.ctrlKey;
}

export function handleChipLinkClick(
  event: { metaKey: boolean; ctrlKey: boolean; preventDefault: () => void },
  onNavigate: (newTab: boolean) => boolean,
  onClose: () => void,
): void {
  const newTab = shouldOpenInNewTab(event);
  if (!onNavigate(newTab)) return;
  event.preventDefault();
  if (!newTab) onClose();
}

function navigateToAnchorHref(anchor: string, locationHash = window.location.hash): void {
  const currentDocName = getCurrentDocNameFromHash(locationHash);
  if (!currentDocName) return;
  navigateToInternalHashHref({ docName: currentDocName, anchor });
}

export function navigateToMarkdownTarget(
  target: ClassifiedLinkTarget,
  locationHash = window.location.hash,
): void {
  if (target.kind === 'doc') {
    navigateToInternalHashHref(target);
    return;
  }

  if (target.kind === 'anchor') {
    navigateToAnchorHref(target.anchor, locationHash);
    return;
  }

  if (target.kind === 'external') {
    openExternalUrl(target.url);
    return;
  }

  openHashHrefInNewTab(target.url);
}

export function buildCurrentRelativeMarkdownHref(
  targetDocName: string,
  anchor: string | null,
  locationHash = window.location.hash,
): string {
  const sourceDocName = getCurrentDocNameFromHash(locationHash);
  return buildRelativeMarkdownHref(sourceDocName, targetDocName, anchor);
}