data-markdown.js 756 B

12345678910111213141516171819202122232425
  1. // From https://gist.github.com/1343518
  2. // Modified by Hakim to handle markdown indented with tabs
  3. (function(){
  4. [].forEach.call( document.querySelectorAll('[data-markdown]'), function fn(elem){
  5. // strip leading whitespace so it isn't evaluated as code
  6. var text = elem.innerHTML;
  7. var leadingWs = text.match(/^\n?(\s*)/)[1].length,
  8. leadingTabs = text.match(/^\n?(\t*)/)[1].length;
  9. if( leadingTabs > 0 ) {
  10. text = text.replace( new RegExp('\\n?\\t{' + leadingTabs + '}','g'), '\n' );
  11. }
  12. else if( leadingWs > 1 ) {
  13. text = text.replace( new RegExp('\\n? {' + leadingWs + '}','g'), '\n' );
  14. }
  15. // here, have sum HTML
  16. elem.innerHTML = (new Showdown.converter()).makeHtml(text);
  17. });
  18. })();