showdown/src/subParsers/encodeAmpsAndAngles.js

18 lines
649 B
JavaScript
Raw Normal View History

2015-01-16 05:21:33 +08:00
/**
* Smart processing for ampersands and angle brackets that need to be encoded.
*/
showdown.subParser('encodeAmpsAndAngles', function (text, options, globals) {
2015-01-19 19:37:21 +08:00
'use strict';
text = globals.converter._dispatch('encodeAmpsAndAngles.before', text, options, globals);
2015-01-19 19:37:21 +08:00
// Ampersand-encoding based entirely on Nat Irons's Amputator MT plugin:
// http://bumppo.net/projects/amputator/
text = text.replace(/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/g, '&');
2015-01-16 05:21:33 +08:00
2015-01-19 19:37:21 +08:00
// Encode naked <'s
text = text.replace(/<(?![a-z\/?\$!])/gi, '&lt;');
2015-01-16 05:21:33 +08:00
text = globals.converter._dispatch('encodeAmpsAndAngles.after', text, options, globals);
2015-01-19 19:37:21 +08:00
return text;
2015-01-16 05:21:33 +08:00
});