UNPKG

reporemix

Version:

A opiniated repomix tool for Rust and NextJS projects.

60 lines (59 loc) 2.79 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.shortenBase64 = shortenBase64; const tree_sitter_parser_1 = require("../tree-sitter-parser"); function shortenBase64(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; // Fallback const stringNodes = []; // Collect string literals function traverse(node) { if (node.type === 'string' || node.type === 'string_literal' || node.type === 'raw_string_literal') { // Adjust per language if (isBase64Like(node.text)) { stringNodes.push(node); } } for (let i = 0; i < node.childCount; i++) { const child = node.child(i); if (child) traverse(child); } } traverse(tree.rootNode); if (stringNodes.length === 0) return content; // Sort reverse and replace stringNodes.sort((a, b) => b.startIndex - a.startIndex); const pieces = []; let lastEnd = content.length; for (const node of stringNodes) { if (node.text.length > 75) { // Extract the data URL parts const match = node.text.match(/^(["'])(data:[^;]+;base64,)(.+?)(["'])$/); if (match) { const [, openQuote, prefix, base64Data, closeQuote] = match; const shortened = `${openQuote}${prefix}${base64Data.slice(0, 5)}...${base64Data.slice(-3)}${closeQuote}`; pieces.push(content.slice(node.endIndex, lastEnd)); pieces.push(shortened); lastEnd = node.startIndex; } } } pieces.push(content.slice(0, lastEnd)); return pieces.reverse().join(''); }); } function isBase64Like(text) { return /^["']data:[^;]+;base64,.+["']$/.test(text); }