Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

7.0 KB
Raw Blame
/** Schema unchanged (precedent #9 add-only). */

import { LinkFidelity } from '@inkeep/open-knowledge-core/extensions/link-fidelity';
import {
  assertNeverLinkTarget,
  classifyMarkdownHref,
  extractAssetExtension,
  resolveAssetProjectPath,
} from '@inkeep/open-knowledge-core/utils/link-targets';
import { type Editor, mergeAttributes } from '@tiptap/core';
import { createElement } from 'react';
import { openExternalUrl } from '@/lib/external-link';
import { resolveLinkTargetIntent } from '../../components/link-target-intent';
import {
  activateAssetLink,
  navigateToMarkdownTarget,
  openInternalHashHrefInNewTab,
} from '../internal-link-helpers';
import { getPageListCache, type PageListCacheSnapshot } from '../page-list-cache';
import { createAssetContextMenuPlugin } from '../plugins/asset-context-menu';
import { isSafeNavigationUrl } from '../safe-navigation-url';
import { InternalLinkPropPanel } from './InternalLinkPropPanel';
import { makeLinkResolutionAttrsComputer, resolveAssetHrefPath } from './link-resolution';
import { linkResolutionDecorationPlugin } from './link-resolution-decoration';
import { createMarkInteractionBridgePlugin, getCurrentMarkInfo } from './mark-interaction-bridge';

export interface InternalLinkOptions {
  docName: string;
}

export type LinkMarkAssetActivation =
  | { kind: 'not-asset' }
  | { kind: 'refused' }
  | { kind: 'asset'; url: string; ext: string; literal: boolean; projectRelPath: string };

export function resolveLinkMarkAssetActivation(params: {
  href: string;
  sourceForm: unknown;
  docName: string;
  classified: ReturnType<typeof classifyMarkdownHref>;
}): LinkMarkAssetActivation {
  const { href, sourceForm, docName, classified } = params;
  const hrefExt = extractAssetExtension(href);
  const isWikiEmbed = sourceForm === 'wikiembed';
  if (classified?.kind !== 'asset' && !(isWikiEmbed && hrefExt !== null)) {
    return { kind: 'not-asset' };
  }
  const url = classified?.kind === 'asset' ? classified.url : href;
  const ext = classified?.kind === 'asset' ? classified.ext : (hrefExt ?? '');
  const literal = isWikiEmbed;
  const projectRelPath = resolveAssetProjectPath(url, docName, { literal });
  if (!projectRelPath) return { kind: 'refused' };
  return { kind: 'asset', url, ext, literal, projectRelPath };
}

type AssetLinkActivationTarget = Omit<Parameters<typeof activateAssetLink>[0], 'newTab'>;

function activationTarget(
  url: string,
  projectRelPath: string,
  ext: string,
): AssetLinkActivationTarget {
  return { url, projectRelPath, ext, title: projectRelPath.split('/').pop() ?? url };
}

export function resolveStoredAssetActivation(
  activation: { url: string; ext: string; literal: boolean; projectRelPath: string },
  docName: string,
  cache: PageListCacheSnapshot,
): AssetLinkActivationTarget | null {
  const { url, ext, literal, projectRelPath } = activation;
  if (cache.assetPaths === undefined && cache.filePaths === undefined) {
    return activationTarget(url, projectRelPath, ext);
  }
  const stored = resolveAssetHrefPath(url, docName, cache.assetPaths, cache.filePaths, { literal });
  return stored === null ? null : activationTarget(url, stored, ext);
}

export function resolveTrackedFileActivation(
  href: string,
  docName: string,
  cache: PageListCacheSnapshot | null,
): AssetLinkActivationTarget | null {
  if (cache === null) return null;
  const stored = resolveAssetHrefPath(href, docName, cache.assetPaths, cache.filePaths, {
    literal: false,
  });
  return stored === null ? null : activationTarget(href, stored, extractAssetExtension(href) ?? '');
}

export const InternalLink = LinkFidelity.extend<InternalLinkOptions>({
  addOptions() {
    return {
      ...this.parent?.(),
      docName: '',
    };
  },

  renderHTML({ HTMLAttributes }) {
    const href = typeof HTMLAttributes.href === 'string' ? HTMLAttributes.href : '';
    const ariaLabel = href ? `Link: ${href}` : 'Link';
    return [
      'span',
      mergeAttributes(HTMLAttributes, {
        'data-link': '',
        role: 'link',
        tabindex: '0',
        'aria-label': ariaLabel,
        style: 'touch-action: manipulation;',
      }),
      0,
    ];
  },

  addProseMirrorPlugins() {
    const docName = this.options.docName ?? '';
    const handlePrimary = ({
      editor,
      nodeId,
      newTab,
    }: {
      editor: Editor;
      nodeId: string;
      newTab: boolean;
    }): boolean => {
      const info = getCurrentMarkInfo(editor.state, nodeId);
      const href = info?.attrs?.href;
      if (typeof href !== 'string' || !href) return false;

      const target = classifyMarkdownHref(href, docName);
      const activation = resolveLinkMarkAssetActivation({
        href,
        sourceForm: info?.attrs?.sourceForm,
        docName,
        classified: target,
      });
      if (activation.kind === 'refused') return false;
      if (activation.kind === 'asset') {
        const cache = getPageListCache();
        if (cache === null) return false;
        const stored = resolveStoredAssetActivation(activation, docName, cache);
        if (stored === null) return false;
        activateAssetLink({ ...stored, newTab });
        return true;
      }

      if (!target) return false;

      switch (target.kind) {
        case 'asset':
          return false;
        case 'doc': {
          const cache = getPageListCache();
          const intent = resolveLinkTargetIntent(target.docName, {
            pages: cache?.pages ?? new Set<string>(),
            folderPaths: cache?.folderPaths ?? new Set<string>(),
          });
          if (intent.kind === 'create') {
            const trackedFile = resolveTrackedFileActivation(href, docName, cache);
            if (trackedFile === null) return false;
            activateAssetLink({ ...trackedFile, newTab });
            return true;
          }
          if (newTab) {
            openInternalHashHrefInNewTab({ docName: target.docName, anchor: target.anchor });
          } else {
            navigateToMarkdownTarget(target);
          }
          return true;
        }
        case 'anchor':
          if (newTab) {
            openInternalHashHrefInNewTab({ docName, anchor: target.anchor });
          } else {
            navigateToMarkdownTarget(target);
          }
          return true;
        case 'external':
          if (!isSafeNavigationUrl(target.url)) return false;
          openExternalUrl(target.url);
          return true;
        default:
          return assertNeverLinkTarget(target);
      }
    };
    return [
      createMarkInteractionBridgePlugin({
        editor: this.editor,
        markTypes: ['link'],
        renderPropPanel: ({ editor, nodeId, deactivate }) =>
          createElement(InternalLinkPropPanel, {
            editor,
            nodeId,
            sourceDocName: docName,
            onClose: deactivate,
            onNavigate: (newTab: boolean) => handlePrimary({ editor, nodeId, newTab }),
          }),
        handlePrimary,
      }),
      linkResolutionDecorationPlugin({
        markTypes: ['link'],
        computeAttrs: makeLinkResolutionAttrsComputer(docName),
      }),
      createAssetContextMenuPlugin({ sourceDocName: docName }),
    ];
  },
});