MCPcopy
hub / github.com/upstash/context7 / parseSkillFrontmatter

Function parseSkillFrontmatter

packages/cli/src/utils/github.ts:96–144  ·  view source on GitHub ↗
(content: string)

Source from the content-addressed store, hash-verified

94}
95
96function parseSkillFrontmatter(content: string): { name: string; description: string } | null {
97 const frontmatterMatch = content.match(/^---\s*\n([\s\S]*?)\n---/);
98 if (!frontmatterMatch) return null;
99
100 const frontmatter = frontmatterMatch[1];
101
102 const nameMatch = frontmatter.match(/^name:\s*(.+)$/m);
103 if (!nameMatch) return null;
104 const name = nameMatch[1].trim().replace(/^["']|["']$/g, "");
105 if (!isSafeSkillName(name)) return null;
106
107 let description = "";
108 const multiLineMatch = frontmatter.match(/^description:\s*([|>])-?\s*$/m);
109
110 if (multiLineMatch) {
111 const descLineIndex = frontmatter.indexOf("description:");
112 const lines = frontmatter.slice(descLineIndex).split("\n").slice(1);
113 const indentedLines: string[] = [];
114 for (const line of lines) {
115 if (line.trim() === "") {
116 indentedLines.push("");
117 continue;
118 }
119 if (/^\s+/.test(line)) {
120 indentedLines.push(line);
121 } else {
122 break;
123 }
124 }
125 const firstNonEmpty = indentedLines.find((l) => l.trim().length > 0);
126 const indent = firstNonEmpty?.match(/^(\s+)/)?.[1].length ?? 0;
127 description = indentedLines
128 .map((line) => line.slice(indent))
129 .join(" ")
130 .replace(/\s+/g, " ")
131 .trim();
132 } else {
133 const singleMatch = frontmatter.match(/^description:\s*(.+)$/m);
134 if (singleMatch) {
135 const value = singleMatch[1].trim();
136 if (!["|", ">", "|-", ">-"].includes(value)) {
137 description = value.replace(/^["']|["']$/g, "");
138 }
139 }
140 }
141
142 if (!description) return null;
143 return { name, description };
144}
145
146function getGitHubHeaders(): Record<string, string> {
147 const ghToken = getGitHubToken();

Callers 1

listSkillsFromGitHubFunction · 0.85

Calls 1

isSafeSkillNameFunction · 0.85

Tested by

no test coverage detected