Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

6.3 KB
Raw Blame
import { MarkdownManager, SAFE_URL_SCHEMES, sharedExtensions } from '@inkeep/open-knowledge-core';
import type { JSONContent } from '@tiptap/core';
import { describe, expect, test } from 'vitest';
import { detectGfmLinkToken, type GfmLinkToken } from './gfm-link-detector';

const mdManager = new MarkdownManager({ extensions: sharedExtensions });

function parseFirstLink(token: string): GfmLinkToken | null {
  function walk(node: JSONContent): GfmLinkToken | null {
    const mark = node.marks?.find((m) => m.type === 'link');
    if (mark) {
      return { href: String(mark.attrs?.href ?? ''), text: node.text ?? '' };
    }
    for (const child of node.content ?? []) {
      const found = walk(child);
      if (found) return found;
    }
    return null;
  }
  return walk(mdManager.parse(token));
}

const MATRIX: Array<{ token: string; expected: GfmLinkToken | null }> = [
  {
    token: 'https://example.com',
    expected: { href: 'https://example.com', text: 'https://example.com' },
  },
  {
    token: 'www.example.com',
    expected: { href: 'http://www.example.com', text: 'www.example.com' },
  },
  { token: '[email protected]', expected: { href: 'mailto:[email protected]', text: '[email protected]' } },
  { token: 'example.com', expected: null },
  { token: 'AGENTS.md', expected: null },
  { token: 'package.json', expected: null },
  { token: 'localhost:5173', expected: null },
  { token: 'foo.bar', expected: null },
  { token: 'v1.2.3', expected: null },
  { token: '192.168.1.1', expected: null },
  {
    token: 'http://localhost:5174',
    expected: { href: 'http://localhost:5174', text: 'http://localhost:5174' },
  },
  {
    token: 'http://localhost:5174/#/doc',
    expected: { href: 'http://localhost:5174/#/doc', text: 'http://localhost:5174/#/doc' },
  },
  { token: 'http://localhost', expected: { href: 'http://localhost', text: 'http://localhost' } },
  {
    token: 'http://127.0.0.1:8080/x',
    expected: { href: 'http://127.0.0.1:8080/x', text: 'http://127.0.0.1:8080/x' },
  },
  { token: 'localhost:5174', expected: null },
  { token: 'http://foo_bar', expected: null },
  {
    token: 'https://example.com.',
    expected: { href: 'https://example.com', text: 'https://example.com' },
  },
  {
    token: 'https://example.com)',
    expected: { href: 'https://example.com', text: 'https://example.com' },
  },
  { token: 'https://foo_bar.com', expected: null },
  { token: '[email protected]_', expected: null },
  { token: '[email protected]', expected: null },
  { token: '[email protected]', expected: null },
  { token: 'a@b_c.com', expected: { href: 'mailto:a@b_c.com', text: 'a@b_c.com' } },
  {
    token: 'www.a_b.example.com',
    expected: { href: 'http://www.a_b.example.com', text: 'www.a_b.example.com' },
  },
  {
    token: 'https://例え.jp',
    expected: { href: 'https://例え.jp', text: 'https://例え.jp' },
  },
  {
    token: 'https://bücher.de/straße',
    expected: { href: 'https://bücher.de/straße', text: 'https://bücher.de/straße' },
  },
  {
    token: 'www.例え.jp',
    expected: { href: 'http://www.例え.jp', text: 'www.例え.jp' },
  },
  {
    token: 'https://пример.рф.',
    expected: { href: 'https://пример.рф', text: 'https://пример.рф' },
  },
];

describe('detectGfmLinkToken — acceptance matrix', () => {
  for (const { token, expected } of MATRIX) {
    test(`${token} → ${expected ? `link ${expected.href}` : 'plain text'}`, () => {
      expect(detectGfmLinkToken(token)).toEqual(expected);
    });
  }
});

describe('detectGfmLinkToken — parity with MarkdownManager.parse', () => {
  const parityTokens = [
    ...MATRIX.map((row) => row.token),
    'http://example.com',
    'HTTPS://Example.com',
    'WWW.Example.com',
    '[email protected]',
    'ftp://files.example.com',
    'https://en.wikipedia.org/wiki/Foo_(bar)',
    'https://ab\u200Ccd.example',
    'https://ab\u200Ccd.ex\u200Cample',
  ];
  for (const token of parityTokens) {
    test(`agrees with the pipeline for ${token}`, () => {
      expect(detectGfmLinkToken(token)).toEqual(parseFirstLink(token));
    });
  }
});

describe('detectGfmLinkToken — bare domains are rejected structurally, not by TLD', () => {
  for (const token of [
    'example.com',
    'foo.bar',
    'a.io',
    'my-site.dev',
    'stripe.com',
    '192.168.1.1',
  ]) {
    test(`${token} stays plain text`, () => {
      expect(detectGfmLinkToken(token)).toBeNull();
    });
  }
});

describe('detectGfmLinkToken — href scheme stays inside the allowlist', () => {
  test('every produced href uses a SAFE_URL_SCHEMES scheme', () => {
    const allowed = SAFE_URL_SCHEMES.map((s) => `${s}:`);
    for (const token of [
      'https://example.com',
      'http://example.com',
      'www.example.com',
      '[email protected]',
    ]) {
      const result = detectGfmLinkToken(token);
      expect(result).not.toBeNull();
      const href = result?.href.toLowerCase() ?? '';
      expect(allowed.some((prefix) => href.startsWith(prefix))).toBe(true);
    }
  });

  test('ftp:// is a safe scheme but not a GFM shape, so it does not convert', () => {
    expect(detectGfmLinkToken('ftp://files.example.com')).toBeNull();
  });
});

describe('detectGfmLinkToken — href shape details', () => {
  test('www. tokens get an http:// href, not https://', () => {
    expect(detectGfmLinkToken('www.example.com')?.href).toBe('http://www.example.com');
  });

  test('scheme match is case-insensitive and preserves original casing', () => {
    expect(detectGfmLinkToken('HTTPS://Example.com')).toEqual({
      href: 'HTTPS://Example.com',
      text: 'HTTPS://Example.com',
    });
    expect(detectGfmLinkToken('WWW.Example.com')?.href).toBe('http://WWW.Example.com');
  });

  test('email tokens become mailto: hrefs with the address as text', () => {
    expect(detectGfmLinkToken('[email protected]')).toEqual({
      href: 'mailto:[email protected]',
      text: '[email protected]',
    });
  });

  test('trailing sentence punctuation is split off the linkified span', () => {
    expect(detectGfmLinkToken('https://example.com.')).toEqual({
      href: 'https://example.com',
      text: 'https://example.com',
    });
  });

  test('a closing paren that balances an opening paren stays in the link', () => {
    expect(detectGfmLinkToken('https://en.wikipedia.org/wiki/Foo_(bar)')?.text).toBe(
      'https://en.wikipedia.org/wiki/Foo_(bar)',
    );
  });

  test('empty string is not a link', () => {
    expect(detectGfmLinkToken('')).toBeNull();
  });
});