MCPcopy Index your code
hub / github.com/codeaashu/claude-code / parseMemoryFileContent

Function parseMemoryFileContent

src/utils/claudemd.ts:343–400  ·  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

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

Callers 1

Calls 6

logForDebuggingFunction · 0.85
parseFrontmatterPathsFunction · 0.85
hasMethod · 0.45

Tested by

no test coverage detected