浏览代码

refactor: extract frontmatter stripping to shared helper function

Jesse Vincent 9 月之前
父节点
当前提交
4eab16380b
共有 2 个文件被更改,包括 34 次插入46 次删除
  1. 2 45
      .opencode/plugin/superpowers.js
  2. 32 1
      lib/skills-core.js

+ 2 - 45
.opencode/plugin/superpowers.js

@@ -34,29 +34,7 @@ module.exports = async ({ project, client, $, directory, worktree }) => {
 
           const fullContent = fs.readFileSync(resolved.skillFile, 'utf8');
           const { name, description } = skillsCore.extractFrontmatter(resolved.skillFile);
-
-          // Extract content after frontmatter
-          const lines = fullContent.split('\n');
-          let inFrontmatter = false;
-          let frontmatterEnded = false;
-          const contentLines = [];
-
-          for (const line of lines) {
-            if (line.trim() === '---') {
-              if (inFrontmatter) {
-                frontmatterEnded = true;
-                continue;
-              }
-              inFrontmatter = true;
-              continue;
-            }
-
-            if (frontmatterEnded || !inFrontmatter) {
-              contentLines.push(line);
-            }
-          }
-
-          const content = contentLines.join('\n').trim();
+          const content = skillsCore.stripFrontmatter(fullContent);
           const skillDirectory = path.dirname(resolved.skillFile);
 
           return `# ${name || skill_name}
@@ -104,28 +82,7 @@ ${content}`;
       let usingSuperpowersContent = '';
       if (usingSuperpowersPath) {
         const fullContent = fs.readFileSync(usingSuperpowersPath.skillFile, 'utf8');
-        // Strip frontmatter
-        const lines = fullContent.split('\n');
-        let inFrontmatter = false;
-        let frontmatterEnded = false;
-        const contentLines = [];
-
-        for (const line of lines) {
-          if (line.trim() === '---') {
-            if (inFrontmatter) {
-              frontmatterEnded = true;
-              continue;
-            }
-            inFrontmatter = true;
-            continue;
-          }
-
-          if (frontmatterEnded || !inFrontmatter) {
-            contentLines.push(line);
-          }
-        }
-
-        usingSuperpowersContent = contentLines.join('\n').trim();
+        usingSuperpowersContent = skillsCore.stripFrontmatter(fullContent);
       }
 
       const toolMapping = `

+ 32 - 1
lib/skills-core.js

@@ -169,9 +169,40 @@ function checkForUpdates(repoDir) {
     }
 }
 
+/**
+ * Strip YAML frontmatter from skill content, returning just the content.
+ *
+ * @param {string} content - Full content including frontmatter
+ * @returns {string} - Content without frontmatter
+ */
+function stripFrontmatter(content) {
+    const lines = content.split('\n');
+    let inFrontmatter = false;
+    let frontmatterEnded = false;
+    const contentLines = [];
+
+    for (const line of lines) {
+        if (line.trim() === '---') {
+            if (inFrontmatter) {
+                frontmatterEnded = true;
+                continue;
+            }
+            inFrontmatter = true;
+            continue;
+        }
+
+        if (frontmatterEnded || !inFrontmatter) {
+            contentLines.push(line);
+        }
+    }
+
+    return contentLines.join('\n').trim();
+}
+
 module.exports = {
     extractFrontmatter,
     findSkillsInDir,
     resolveSkillPath,
-    checkForUpdates
+    checkForUpdates,
+    stripFrontmatter
 };