diff --git a/src/bookmark-processor.js b/src/bookmark-processor.js index e42853d..944d2c8 100644 --- a/src/bookmark-processor.js +++ b/src/bookmark-processor.js @@ -72,9 +72,14 @@ const ICON_MAPPING = { // 获取最新的书签文件 function getLatestBookmarkFile() { try { + console.log('[DEBUG] 开始查找书签文件...'); + console.log('[DEBUG] 书签目录:', BOOKMARKS_DIR); + // 确保书签目录存在 if (!fs.existsSync(BOOKMARKS_DIR)) { + console.log('[DEBUG] 书签目录不存在,创建目录...'); fs.mkdirSync(BOOKMARKS_DIR, { recursive: true }); + console.log('[WARN] 书签目录为空,未找到HTML文件'); return null; } @@ -82,7 +87,13 @@ function getLatestBookmarkFile() { const files = fs.readdirSync(BOOKMARKS_DIR) .filter(file => file.toLowerCase().endsWith('.html')); + console.log('[DEBUG] 找到的HTML文件数量:', files.length); + if (files.length > 0) { + console.log('[DEBUG] HTML文件列表:', files); + } + if (files.length === 0) { + console.log('[WARN] 未找到任何HTML书签文件'); return null; } @@ -95,16 +106,23 @@ function getLatestBookmarkFile() { // 找出最新的文件 fileStats.sort((a, b) => b.mtime - a.mtime); const latestFile = fileStats[0].file; + const latestFilePath = path.join(BOOKMARKS_DIR, latestFile); - return path.join(BOOKMARKS_DIR, latestFile); + console.log('[INFO] 选择最新的书签文件:', latestFile); + console.log('[DEBUG] 完整路径:', latestFilePath); + + return latestFilePath; } catch (error) { - console.error('Error finding bookmark file:', error); + console.error('[ERROR] 查找书签文件时出错:', error); return null; } } // 解析书签HTML内容,支持2-4层级嵌套结构 function parseBookmarks(htmlContent) { + console.log('[DEBUG] 开始解析书签HTML内容...'); + console.log('[DEBUG] HTML内容长度:', htmlContent.length, '字符'); + // 正则表达式匹配文件夹和书签 const folderRegex = /
/gi); - const dlEnd = htmlContent.substring(pos).match(/<\/DL>
/gi); + loopCount++; + if (loopCount > maxLoops) { + console.error(`[ERROR] 检测到可能的无限循环! 文件夹:"${folderName}", 层级:${level}, 循环次数:${loopCount}`); + console.error(`[ERROR] 当前位置:${pos}, 深度:${depth}`); + console.error(`[ERROR] 周围内容:`, htmlContent.substring(pos, pos + 100)); + break; + } - if (dlStart && dlStart.index < (dlEnd ? dlEnd.index : htmlContent.length)) { + // 修复:使用 search() 而不是 match(),因为 match() 返回数组没有 index 属性 + const remainingContent = htmlContent.substring(pos); + const dlStartIndex = remainingContent.search(/
/i); + const dlEndIndex = remainingContent.search(/<\/DL>
/i); + + if (loopCount % 100 === 0) { + console.log(`[DEBUG] 循环 ${loopCount}: pos=${pos}, depth=${depth}, dlStart=${dlStartIndex}, dlEnd=${dlEndIndex}`); + } + + // 找到开始标签且在结束标签之前(或没有结束标签) + if (dlStartIndex !== -1 && (dlEndIndex === -1 || dlStartIndex < dlEndIndex)) { depth++; - pos += dlStart.index + dlStart[0].length; - } else if (dlEnd) { + pos += dlStartIndex + '
'.length; + console.log(`[DEBUG] 找到
在位置 ${pos}, depth增加到 ${depth}`); + } + // 找到结束标签 + else if (dlEndIndex !== -1) { depth--; - pos += dlEnd.index + dlEnd[0].length; - } else { + pos += dlEndIndex + '
'.length; + console.log(`[DEBUG] 找到
在位置 ${pos}, depth减少到 ${depth}`); + } + // 都没找到,退出循环 + else { + console.log(`[DEBUG] 未找到更多标签,退出循环`); break; } } + if (loopCount > 100) { + console.log(`[DEBUG] 文件夹"${folderName}"边界查找循环${loopCount}次`); + } + folderContentEnd = pos; const folderContent = htmlContent.substring(folderEnd, folderContentEnd); + console.log(`[DEBUG] 文件夹"${folderName}"内容长度: ${folderContent.length}`); + // 解析文件夹内容 const folder = { name: folderName, @@ -157,14 +213,19 @@ function parseBookmarks(htmlContent) { path: [...parentPath, folderName] }; - // 检查是否包含子文件夹 - const hasSubfolders = folderRegex.test(folderContent); - folderRegex.lastIndex = 0; + // 检查是否包含子文件夹 - 创建新的正则实例避免干扰主循环 + const testFolderRegex = /