I wrote a client side script that pretty much does what you want. It is divided into a description question and an HTML block. You will find it at https://moodleformulas.org/course/view.php?id=77§ion=57. It is a draft but it works. Like any client-side script, it's not immune to computer savvy students who might outsmart it. The script is a bit complex and gives an idea of the complexity of a real plugin that would do the job well. The first possibility would therefore seem rather laborious, and I will not go there. The second possibility still seems the easiest.