UNPKG

reporemix

Version:

A opiniated repomix tool for Rust and NextJS projects.

65 lines (64 loc) 3.52 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.replaceSvgContent = replaceSvgContent; const node_path_1 = __importDefault(require("node:path")); const web_tree_sitter_1 = require("web-tree-sitter"); const tree_sitter_parser_1 = require("../tree-sitter-parser"); function replaceSvgContent(content, filePath) { return __awaiter(this, void 0, void 0, function* () { const tree = yield tree_sitter_parser_1.treeSitterParser.parseFile(content, filePath); if (!tree) return content; const language = yield tree_sitter_parser_1.treeSitterParser.getLanguageForFile(filePath); if (!language) return content; // Only apply SVG replacement to JSX/TSX files const ext = node_path_1.default.extname(filePath).toLowerCase(); if (!['.jsx', '.tsx'].includes(ext)) { return content; // Skip non-JSX files } try { const querySource = '(jsx_element (jsx_opening_element name: (identifier) @tag (#eq? @tag "svg")) ) @svg'; const query = new web_tree_sitter_1.Query(language, querySource); const matches = query.matches(tree.rootNode); let newContent = content; const replacements = []; for (const match of matches) { const svgNode = match.captures[0].node; // The @svg capture // Find the opening and closing tags to preserve attributes const opening = svgNode.child(0); // jsx_opening_element const closing = svgNode.child(svgNode.childCount - 1); // jsx_closing_element if (!opening || !closing) continue; // Replace content between opening and closing const attributes = content.slice(opening.startIndex, opening.endIndex); const replacement = `${attributes}>[Removed]</svg>`; replacements.push({ start: opening.startIndex, end: closing.endIndex, text: replacement }); } // Apply replacements in reverse order replacements.sort((a, b) => b.start - a.start); for (const rep of replacements) { newContent = newContent.slice(0, rep.start) + rep.text + newContent.slice(rep.end); } return newContent; } catch (error) { // If query fails (e.g., language doesn't support JSX), return original content const errorMessage = error instanceof Error ? error.message : String(error); console.warn(`SVG replacement skipped for ${filePath}: ${errorMessage}`); return content; } }); }