I’m not that good with regex. But it would sure be a whole lot faster.
I just finished it, and did the exact same thing you came up with.
I broke down the content into an array, tagging some elements as non-parseable. Then as I added links, I had to build a temp array splitting lines up even further.
It’s not a high traffic site, otherwise I’d be seriously concerned about speed, but it at least parses correctly now.
It’s certainly not elegant code, but it works.