@@ -2643,9 +2643,12 @@ def run(self, text):
26432643 text = self .strong_re .sub (self .sub , text )
26442644 text = self .em_re .sub (self .sub , text )
26452645 else :
2646- # put any hashed values back
2647- for key , substr in self .hash_table .items ():
2648- text = text .replace (key , substr )
2646+ # push any hashed values back, using a while loop to deal with recursive hashes
2647+ orig_text = ''
2648+ while orig_text != text :
2649+ orig_text = text
2650+ for key , substr in self .hash_table .items ():
2651+ text = text .replace (key , substr )
26492652 return text
26502653
26512654 @abstractmethod
@@ -2762,12 +2765,20 @@ class CodeFriendly(ItalicAndBoldProcessor):
27622765
27632766 def sub (self , match : re .Match ) -> str :
27642767 syntax = match .group (1 )
2765- if '_' not in syntax :
2766- return super ().sub (match )
2767- text = match .string [match .start (): match .end ()]
2768- key = _hash_text (text )
2769- self .hash_table [key ] = text
2770- return key
2768+ text : str = match .string [match .start (): match .end ()]
2769+ if '_' in syntax :
2770+ # if using _this_ syntax, hash the whole thing so that it doesn't get processed
2771+ key = _hash_text (text )
2772+ self .hash_table [key ] = text
2773+ return key
2774+ elif '_' in text :
2775+ # if the text within the bold/em markers contains '_' then hash those contents to protect them from em_re
2776+ text = text [len (syntax ): - len (syntax )]
2777+ key = _hash_text (text )
2778+ self .hash_table [key ] = text
2779+ return syntax + key + syntax
2780+ # if no underscores are present, the text is fine and we can just leave it alone
2781+ return super ().sub (match )
27712782
27722783
27732784class FencedCodeBlocks (Extra ):
0 commit comments