I wrote a bit of Javascript that converts Pinyin with numbers to pinyin with tone marks. A demo is available on my website: http://shinyandnew.ca/pinyin_fixer.html
To use it, just type in the textarea like this
Ni3 hao3! Ta1 shi4 wo3de nv3peng2you2!
And it outputs
Nĭ hăo! Tā shì wǒde nǚpéngyóu!
The Javascript code on that page should be easy enough to understand and modify. It is currently a bit buggy but the algorithm for modifying a single word that contains pinyin-approved characters is sound.
I propose that this code be added to the markdown editor, perhaps with a flag to turn it on and off, so that it can be used easily while typing. It solves the problem of knowing which letter gets the tone, which characters are used for the tone marks, and how to input ü
. It also works with capital letters. With a bit of tweaking it can be enhanced to work properly on letters as the words are typed.
Bugs:
- It rescans the whole text with every keyup/down.
- It doesn't allow you to write anything with the letter v in it.
- While editing text in the middle (as opposed to appending at the end) it causes your cursor to jump around.
- Probably many more.
But these are integration issues that would have to be addressed for the markdown anyway. In fact, maybe it could skip changing what the user is typing and simply apply in a formatting pass, the way bold and italics are applied now. Either way, I present that JavaScript file with its algorithm for use here.