MCPcopy Index your code
hub / github.com/Noumena-Network/code / parseMemoryFileContent

Function parseMemoryFileContent

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

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

Callers 1

Calls 6

parseFrontmatterPathsFunction · 0.85
logForDebuggingFunction · 0.70
hasMethod · 0.45

Tested by

no test coverage detected