File size: 1,788 Bytes
61aec16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
 * T072: Extract wikilinks from markdown text
 * Matches [[link text]] pattern
 */
export function extractWikilinks(text: string): string[] {
  const pattern = /\[\[([^\]]+)\]\]/g;
  const matches: string[] = [];
  let match;
  
  while ((match = pattern.exec(text)) !== null) {
    matches.push(match[1]);
  }
  
  return matches;
}

/**
 * T073: Normalize text to a URL-safe slug
 * - Lowercase
 * - Replace spaces and underscores with dashes
 * - Strip non-alphanumeric except dashes
 */
export function normalizeSlug(text: string): string {
  return text
    .toLowerCase()
    .replace(/[\s_]+/g, '-')
    .replace(/[^a-z0-9-]/g, '')
    .replace(/-+/g, '-')
    .replace(/^-|-$/g, '');
}

/**
 * Parse wikilink syntax in markdown text
 * Returns array of { text, linkText } objects
 */
export function parseWikilinks(text: string): Array<{ text: string; linkText: string | null }> {
  const parts: Array<{ text: string; linkText: string | null }> = [];
  const pattern = /\[\[([^\]]+)\]\]/g;
  let lastIndex = 0;
  let match;

  while ((match = pattern.exec(text)) !== null) {
    // Add text before the wikilink
    if (match.index > lastIndex) {
      parts.push({
        text: text.slice(lastIndex, match.index),
        linkText: null,
      });
    }

    // Add the wikilink
    parts.push({
      text: match[0],
      linkText: match[1],
    });

    lastIndex = pattern.lastIndex;
  }

  // Add remaining text
  if (lastIndex < text.length) {
    parts.push({
      text: text.slice(lastIndex),
      linkText: null,
    });
  }

  return parts;
}

/**
 * Convert wikilink text to a probable note path
 * Adds .md extension and normalizes
 */
export function wikilinkToPath(linkText: string): string {
  const slug = normalizeSlug(linkText);
  return `${slug}.md`;
}