Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112// Package planparse extracts skill and LSP references declared in a Claude// Code plan's "skills to load" section. Plan markdown is the source; the// returned slices are ordered, de-duplicated, and split by whether the entry// names a Skill or an LSP server.package planparse
import ( "regexp" "strings")
// headingRe matches a markdown heading whose text introduces a skills or// prerequisites section. The capture group holds the raw heading text minus// the leading hashes and whitespace; isSkillsHeading inspects it.var headingRe = regexp.MustCompile(`^#{1,6}\s+(.*?)\s*$`)
// skillsHeadingRe matches the recognized heading variants surveyed in the// plan corpus. Anchored so titles that merely begin with "Skills" do not// match.var skillsHeadingRe = regexp.MustCompile(`(?i)^(skills?|required\s+(skills?|lsps?)|prerequisites?)(\s+(to\s+load|loaded|in\s+use))?(\s+\(.*\))?(\s+(before\s+coding|on\s+execution|for\s+\w+|when\s+executing.*))?(\s+(and\s+lsps?|/\s*lsps?))?$`)
// lspOnlyHeadingRe matches a heading that names LSPs exclusively. Tokens// found under such a heading default to the lsps slice without needing an// "LSP:" prefix.var lspOnlyHeadingRe = regexp.MustCompile(`(?i)^required\s+lsps?$`)
// backtickRefRe matches a backtick-wrapped token inside markdown text.var backtickRefRe = regexp.MustCompile("`([^`]+)`")
// bulletRe matches a list bullet line and captures the bullet body.var bulletRe = regexp.MustCompile(`^\s*[-*+]\s+(.*)$`)
// tableRowRe matches a markdown table row line, capturing the inner cells.var tableRowRe = regexp.MustCompile(`^\s*\|(.*)\|\s*$`)
// tableSeparatorRe matches a markdown table separator row (only dashes,// pipes, colons, and whitespace).var tableSeparatorRe = regexp.MustCompile(`^\s*\|[\s\-:|]+\|\s*$`)
// lspPrefixRe strips a leading "LSP:" marker (case-insensitive) from a cell.var lspPrefixRe = regexp.MustCompile(`(?i)^\s*LSP:\s*`)
// fenceRe matches a fenced code block delimiter line.var fenceRe = regexp.MustCompile("^\\s*(```|~~~)")
// ExtractRefs scans plan markdown for sections declaring skills or LSPs to// load and returns each set in declaration order with duplicates removed.func ExtractRefs(planBody []byte) (skills, lsps []string) { lines := strings.Split(string(planBody), "\n") seenSkill := map[string]bool{} seenLSP := map[string]bool{} inSection := false sectionLSPOnly := false inFence := false for _, line := range lines { if fenceRe.MatchString(line) { inFence = !inFence continue } if inFence { continue } if h := headingRe.FindStringSubmatch(line); len(h) == 2 { inSection = skillsHeadingRe.MatchString(h[1]) sectionLSPOnly = inSection && lspOnlyHeadingRe.MatchString(h[1]) continue } if !inSection { continue } if tableSeparatorRe.MatchString(line) { continue } if row := tableRowRe.FindStringSubmatch(line); row != nil { cells := strings.Split(row[1], "|") if len(cells) == 0 { continue } extractFromCell(cells[0], sectionLSPOnly, &skills, &lsps, seenSkill, seenLSP) continue } if bullet := bulletRe.FindStringSubmatch(line); bullet != nil { extractFromCell(bullet[1], sectionLSPOnly, &skills, &lsps, seenSkill, seenLSP) } } return skills, lsps}
// extractFromCell pulls every backtick-wrapped token out of cell text and// routes it to skills or lsps. Tokens carry an LSP designation when the cell// has a leading "LSP:" marker or when the enclosing section heading names// LSPs exclusively.func extractFromCell(cell string, sectionLSPOnly bool, skills, lsps *[]string, seenSkill, seenLSP map[string]bool) { isLSP := sectionLSPOnly || lspPrefixRe.MatchString(cell) for _, m := range backtickRefRe.FindAllStringSubmatch(cell, -1) { ref := m[1] if isLSP { if seenLSP[ref] { continue } seenLSP[ref] = true *lsps = append(*lsps, ref) continue } if seenSkill[ref] { continue } seenSkill[ref] = true *skills = append(*skills, ref) }}