MCPcopy Create free account
hub / github.com/Openpanel-dev/openpanel / processFile

Function processFile

scripts/add-internal-links.mjs:141–217  ·  view source on GitHub ↗
(filePath, dir)

Source from the content-addressed store, hash-verified

139// ── Core processing ──────────────────────────────────────────────────
140
141function processFile(filePath, dir) {
142 let content = fs.readFileSync(filePath, 'utf8');
143
144 const skipZones = getSkipZones(content);
145 const changes = [];
146
147 for (const feature of FEATURES) {
148 // If the file already links to this feature URL, skip entirely
149 if (content.includes(feature.url)) continue;
150
151 let linked = false;
152
153 for (const pattern of feature.patterns) {
154 if (linked) break;
155
156 const re = new RegExp(`\\b${escapeRegex(pattern)}\\b`, 'gi');
157 let m;
158
159 while ((m = re.exec(content))) {
160 // In a skip zone?
161 if (overlapsSkipZone(m.index, m[0].length, skipZones)) continue;
162
163 // Check excludeBefore / excludeAfter
164 if (feature.excludeBefore) {
165 const before = content.slice(Math.max(0, m.index - 20), m.index).toLowerCase();
166 if (feature.excludeBefore.some((w) => before.endsWith(w + ' '))) continue;
167 }
168 if (feature.excludeAfter) {
169 const after = content.slice(m.index + m[0].length, m.index + m[0].length + 20).toLowerCase();
170 if (feature.excludeAfter.some((w) => after.startsWith(' ' + w))) continue;
171 }
172
173 // Build replacement
174 const replacement = `[${m[0]}](/features/${feature.slug})`;
175 content =
176 content.slice(0, m.index) +
177 replacement +
178 content.slice(m.index + m[0].length);
179
180 // Add the new link as a skip zone and shift all subsequent zones
181 const lenDiff = replacement.length - m[0].length;
182 skipZones.push({ start: m.index, end: m.index + replacement.length });
183 for (const z of skipZones) {
184 if (z.start > m.index + m[0].length) {
185 z.start += lenDiff;
186 z.end += lenDiff;
187 }
188 }
189
190 changes.push({ feature: feature.slug, matched: m[0] });
191 linked = true;
192 break;
193 }
194 }
195 }
196
197 if (changes.length === 0) return null;
198

Callers 1

Calls 6

getSkipZonesFunction · 0.85
escapeRegexFunction · 0.85
overlapsSkipZoneFunction · 0.85
replaceMethod · 0.80
someMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected