@@ -3,9 +3,9 @@ export type TextDirection = "ltr" | "rtl";
33const LETTER_CHARACTER = / ^ \p{ Letter} $ / u;
44const RTL_SCRIPT_CHARACTER =
55 / ^ [ \u0590 - \u08ff \ufb1d - \ufdff \ufe70 - \ufeff \u{10800} - \u{10fff} \u{1e800} - \u{1eeff} ] $ / u;
6- const INLINE_MARKDOWN_CODE = / ` [ ^ ` \n ] * ` / g;
76const GITHUB_ALERT_MARKER =
87 / ^ { 0 , 3 } (?: > { 0 , 3 } ) + \[ ! (?: N O T E | T I P | I M P O R T A N T | W A R N I N G | C A U T I O N ) \] [ \t ] * $ / gimu;
8+ const INDENTED_CODE_LINE = / ^ (?: { 4 } | \t | { 0 , 3 } (?: > ? ) + (?: { 4 } | \t ) ) / ;
99
1010function stripFencedMarkdownCode ( markdown : string ) : string {
1111 let fence : { character : "`" | "~" ; length : number } | undefined ;
@@ -22,7 +22,7 @@ function stripFencedMarkdownCode(markdown: string): string {
2222 continue ;
2323 }
2424
25- proseLines . push ( line ) ;
25+ if ( ! INDENTED_CODE_LINE . test ( line ) ) proseLines . push ( line ) ;
2626 continue ;
2727 }
2828
@@ -40,6 +40,47 @@ function stripFencedMarkdownCode(markdown: string): string {
4040 return proseLines . join ( "\n" ) ;
4141}
4242
43+ function stripInlineMarkdownCode ( markdown : string ) : string {
44+ let prose = "" ;
45+ let index = 0 ;
46+
47+ while ( index < markdown . length ) {
48+ if ( markdown [ index ] !== "`" ) {
49+ prose += markdown [ index ] ;
50+ index += 1 ;
51+ continue ;
52+ }
53+
54+ const openingStart = index ;
55+ while ( markdown [ index ] === "`" ) index += 1 ;
56+ const delimiterLength = index - openingStart ;
57+ let searchIndex = index ;
58+ let closingEnd : number | undefined ;
59+
60+ while ( searchIndex < markdown . length ) {
61+ const closingStart = markdown . indexOf ( "`" , searchIndex ) ;
62+ if ( closingStart === - 1 ) break ;
63+
64+ let runEnd = closingStart ;
65+ while ( markdown [ runEnd ] === "`" ) runEnd += 1 ;
66+ if ( runEnd - closingStart === delimiterLength ) {
67+ closingEnd = runEnd ;
68+ break ;
69+ }
70+ searchIndex = runEnd ;
71+ }
72+
73+ if ( closingEnd !== undefined ) {
74+ index = closingEnd ;
75+ continue ;
76+ }
77+
78+ prose += markdown . slice ( openingStart , index ) ;
79+ }
80+
81+ return prose ;
82+ }
83+
4384export function resolveTextDirection ( text : string ) : TextDirection {
4485 for ( const character of text ) {
4586 if ( ! LETTER_CHARACTER . test ( character ) ) continue ;
@@ -50,8 +91,6 @@ export function resolveTextDirection(text: string): TextDirection {
5091
5192export function resolveMarkdownTextDirection ( markdown : string ) : TextDirection {
5293 return resolveTextDirection (
53- stripFencedMarkdownCode ( markdown )
54- . replace ( GITHUB_ALERT_MARKER , "" )
55- . replace ( INLINE_MARKDOWN_CODE , "" ) ,
94+ stripInlineMarkdownCode ( stripFencedMarkdownCode ( markdown ) . replace ( GITHUB_ALERT_MARKER , "" ) ) ,
5695 ) ;
5796}
0 commit comments