MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / parseMemoryFileContent

Function parseMemoryFileContent

source code/utils/claudemd.ts:345–402  ·  view source on GitHub ↗

* Parses raw memory file content into a MemoryFileInfo. Pure function — no I/O. * * When includeBasePath is given, @include paths are resolved in the same lex * pass and returned alongside the parsed file (so processMemoryFile doesn't * need to lex the same content a second time).

(
  rawContent: string,
  filePath: string,
  type: MemoryType,
  includeBasePath?: string,
)

Source from the content-addressed store, hash-verified

343 * need to lex the same content a second time).
344 */
345function parseMemoryFileContent(
346 rawContent: string,
347 filePath: string,
348 type: MemoryType,
349 includeBasePath?: string,
350): { info: MemoryFileInfo | null; includePaths: string[] } {
351 // Skip non-text files to prevent loading binary data (images, PDFs, etc.) into memory
352 const ext = extname(filePath).toLowerCase()
353 if (ext && !TEXT_FILE_EXTENSIONS.has(ext)) {
354 logForDebugging(`Skipping non-text file in @include: ${filePath}`)
355 return { info: null, includePaths: [] }
356 }
357
358 const { content: withoutFrontmatter, paths } =
359 parseFrontmatterPaths(rawContent)
360
361 // Lex once so strip and @include-extract share the same tokens. gfm:false
362 // is required by extract (so ~/path doesn't tokenize as strikethrough) and
363 // doesn't affect strip (html blocks are a CommonMark rule).
364 const hasComment = withoutFrontmatter.includes('<!--')
365 const tokens =
366 hasComment || includeBasePath !== undefined
367 ? new Lexer({ gfm: false }).lex(withoutFrontmatter)
368 : undefined
369
370 // Only rebuild via tokens when a comment actually needs stripping —
371 // marked normalises \r\n during lex, so round-tripping a CRLF file
372 // through token.raw would spuriously flip contentDiffersFromDisk.
373 const strippedContent =
374 hasComment && tokens
375 ? stripHtmlCommentsFromTokens(tokens).content
376 : withoutFrontmatter
377
378 const includePaths =
379 tokens && includeBasePath !== undefined
380 ? extractIncludePathsFromTokens(tokens, includeBasePath)
381 : []
382
383 // Truncate MEMORY.md entrypoints to the line AND byte caps
384 let finalContent = strippedContent
385 if (type === 'AutoMem' || type === 'TeamMem') {
386 finalContent = truncateEntrypointContent(strippedContent).content
387 }
388
389 // Covers frontmatter strip, HTML comment strip, and MEMORY.md truncation
390 const contentDiffersFromDisk = finalContent !== rawContent
391 return {
392 info: {
393 path: filePath,
394 type,
395 content: finalContent,
396 globs: paths,
397 contentDiffersFromDisk,
398 rawContent: contentDiffersFromDisk ? rawContent : undefined,
399 },
400 includePaths,
401 }
402}

Callers 1

Calls 6

logForDebuggingFunction · 0.85
parseFrontmatterPathsFunction · 0.85
hasMethod · 0.45

Tested by

no test coverage detected