source dump of claude code
at main 51 lines 2.3 kB view raw
1/** 2 * Matches any XML-like `<tag>…</tag>` block (lowercase tag names, optional 3 * attributes, multi-line content). Used to strip system-injected wrapper tags 4 * from display titles — IDE context, slash-command markers, hook output, 5 * task notifications, channel messages, etc. A generic pattern avoids 6 * maintaining an ever-growing allowlist that falls behind as new notification 7 * types are added. 8 * 9 * Only matches lowercase tag names (`[a-z][\w-]*`) so user prose mentioning 10 * JSX/HTML components ("fix the <Button> layout", "<!DOCTYPE html>") passes 11 * through — those start with uppercase or `!`. The non-greedy body with a 12 * backreferenced closing tag keeps adjacent blocks separate; unpaired angle 13 * brackets ("when x < y") don't match. 14 */ 15const XML_TAG_BLOCK_PATTERN = /<([a-z][\w-]*)(?:\s[^>]*)?>[\s\S]*?<\/\1>\n?/g 16 17/** 18 * Strip XML-like tag blocks from text for use in UI titles (/rewind, /resume, 19 * bridge session titles). System-injected context — IDE metadata, hook output, 20 * task notifications — arrives wrapped in tags and should never surface as a 21 * title. 22 * 23 * If stripping would result in empty text, returns the original unchanged 24 * (better to show something than nothing). 25 */ 26export function stripDisplayTags(text: string): string { 27 const result = text.replace(XML_TAG_BLOCK_PATTERN, '').trim() 28 return result || text 29} 30 31/** 32 * Like stripDisplayTags but returns empty string when all content is tags. 33 * Used by getLogDisplayTitle to detect command-only prompts (e.g. /clear) 34 * so they can fall through to the next title fallback, and by extractTitleText 35 * to skip pure-XML messages during bridge title derivation. 36 */ 37export function stripDisplayTagsAllowEmpty(text: string): string { 38 return text.replace(XML_TAG_BLOCK_PATTERN, '').trim() 39} 40 41const IDE_CONTEXT_TAGS_PATTERN = 42 /<(ide_opened_file|ide_selection)(?:\s[^>]*)?>[\s\S]*?<\/\1>\n?/g 43 44/** 45 * Strip only IDE-injected context tags (ide_opened_file, ide_selection). 46 * Used by textForResubmit so UP-arrow resubmit preserves user-typed content 47 * including lowercase HTML like `<code>foo</code>` while dropping IDE noise. 48 */ 49export function stripIdeContextTags(text: string): string { 50 return text.replace(IDE_CONTEXT_TAGS_PATTERN, '').trim() 51}