@@ -10,6 +10,17 @@ const {
1010
1111/** Token looks like a file name (e.g. README.md, package.json, Makefile); suggest backticks instead of case change. */
1212const RE_FILE_LIKE = / ^ [ ^ \s ` ] * [ A - Z a - z ] [ ^ \s ` ] * \. [ A - Z a - z 0 - 9 ] + $ / ;
13+
14+ /** Match HTML/XML entity (e.g. ⇒ … →) so we skip title-case checks (ASCII-safe special chars). */
15+ const RE_HTML_ENTITY = / ^ [ ^ A - Z a - z 0 - 9 ] * & (?: [ a - z A - Z ] [ a - z A - Z 0 - 9 ] * | # \d + | # x [ 0 - 9 a - f A - F ] + ) ; [ ^ A - Z a - z 0 - 9 ] * $ / ;
16+
17+ /** Placeholder sentinels for inline code in applyTitleCase (private-use Unicode, not control chars). */
18+ const PLACEHOLDER_START = "\uE000" ;
19+ const PLACEHOLDER_END = "\uE001" ;
20+
21+ function isHtmlEntity ( segment ) {
22+ return typeof segment === "string" && RE_HTML_ENTITY . test ( segment . trim ( ) ) ;
23+ }
1324const FILENAME_NO_EXT = new Set ( [ "makefile" , "dockerfile" , "dockerignore" , "gitignore" , "gitattributes" , "npmrc" , "editorconfig" ] ) ;
1425
1526function looksLikeFileName ( token ) {
@@ -166,6 +177,7 @@ function isPhaseLabelSegment(j, core, previousWordCore) {
166177function checkOneSegment ( opts ) {
167178 const { words, rawSegments, i, j, wordIsSubphraseStart, lowercaseWords, previousWordCore } = opts ;
168179 const rawSeg = rawSegments [ j ] ;
180+ if ( isHtmlEntity ( rawSeg ) ) return null ;
169181 const core = stripWordPunctuation ( rawSeg ) ;
170182 if ( ! core || ! / [ a - z A - Z ] / . test ( core ) ) return null ;
171183
@@ -306,6 +318,10 @@ function processWordForTitleCase(raw, i, words, lowercaseWords) {
306318 const segmentParts = [ ] ;
307319 for ( let j = 0 ; j < rawSegments . length ; j ++ ) {
308320 const rawSeg = rawSegments [ j ] ;
321+ if ( isHtmlEntity ( rawSeg ) ) {
322+ segmentParts . push ( rawSeg ) ;
323+ continue ;
324+ }
309325 const core = stripWordPunctuation ( rawSeg ) ;
310326 if ( ! core || ! / [ a - z A - Z ] / . test ( core ) ) {
311327 segmentParts . push ( rawSeg ) ;
@@ -324,23 +340,81 @@ function processWordForTitleCase(raw, i, words, lowercaseWords) {
324340 return segmentParts . join ( "-" ) ;
325341}
326342
343+ /**
344+ * Replace inline code spans with placeholders so applyTitleCase can preserve them.
345+ * @param {string } line - Raw line
346+ * @returns {{ text: string, spans: string[] } }
347+ */
348+ function replaceInlineCodeWithPlaceholders ( line ) {
349+ const spans = [ ] ;
350+ let out = "" ;
351+ let inCode = false ;
352+ let fence = "" ;
353+ let spanStart = - 1 ;
354+
355+ for ( let i = 0 ; i < line . length ; i ++ ) {
356+ const ch = line [ i ] ;
357+ if ( ch !== "`" ) {
358+ if ( ! inCode ) out += ch ;
359+ continue ;
360+ }
361+
362+ let j = i ;
363+ while ( j < line . length && line [ j ] === "`" ) {
364+ j ++ ;
365+ }
366+ const run = line . slice ( i , j ) ;
367+
368+ if ( ! inCode ) {
369+ inCode = true ;
370+ fence = run ;
371+ spanStart = i ;
372+ i = j - 1 ;
373+ continue ;
374+ }
375+ if ( run === fence ) {
376+ const span = line . slice ( spanStart , j ) ;
377+ spans . push ( span ) ;
378+ out += PLACEHOLDER_START + ( spans . length - 1 ) + PLACEHOLDER_END ;
379+ inCode = false ;
380+ fence = "" ;
381+ i = j - 1 ;
382+ }
383+ }
384+
385+ return { text : out , spans } ;
386+ }
387+
388+ /**
389+ * Restore inline code spans from placeholders in applyTitleCase output.
390+ * @param {string } text - Text with \u0000N\u0000 placeholders
391+ * @param {string[] } spans - Original inline code spans
392+ * @returns {string }
393+ */
394+ function restoreInlineCodePlaceholders ( text , spans ) {
395+ if ( spans . length === 0 ) return text ;
396+ return text . replace ( / \uE000 ( \d + ) \uE001 / g, ( _ , n ) => spans [ Number ( n ) ] ?? "" ) ;
397+ }
398+
327399/**
328400 * Apply AP title case to a title string and return the full corrected title.
329- * Uses the same word/segment logic and getCorrectedSegment as the rule.
401+ * Preserves inline code (backtick spans) unchanged. Uses the same word/segment
402+ * logic and getCorrectedSegment as the rule.
330403 *
331404 * @param {string } titleText - Raw title (e.g. "the quick Brown" or "getting started")
332405 * @param {{ lowercaseWords?: string[]|Set<string>, lowercaseWordsReplaceDefault?: boolean } } [options] - Optional config; defaults to DEFAULT_LOWERCASE_WORDS when not provided
333406 * @returns {string } Title with AP capitalization applied
334407 */
335408function applyTitleCase ( titleText , options = { } ) {
336409 const lowercaseWords = getLowercaseWordsFromOptions ( options ) ;
337- const withCodeStripped = stripInlineCode ( titleText ) ;
338- const words = withCodeStripped . split ( / \s + / ) . filter ( ( w ) => w . length > 0 ) ;
410+ const { text : withPlaceholders , spans } = replaceInlineCodeWithPlaceholders ( titleText ) ;
411+ const words = withPlaceholders . split ( / \s + / ) . filter ( ( w ) => w . length > 0 ) ;
339412 if ( words . length === 0 ) return titleText ;
340413 const resultParts = words . map ( ( raw , i ) =>
341414 processWordForTitleCase ( raw , i , words , lowercaseWords )
342415 ) ;
343- return resultParts . join ( " " ) ;
416+ const joined = resultParts . join ( " " ) ;
417+ return restoreInlineCodePlaceholders ( joined , spans ) ;
344418}
345419
346420/**
0 commit comments