data-markdown.js 923 B

12345678910111213141516171819202122232425262728
  1. // From https://gist.github.com/1343518
  2. // Modified by Hakim to handle Markdown indented with tabs
  3. (function(){
  4. var sections = document.querySelectorAll( '[data-markdown]' );
  5. for( var i = 0, len = sections.length; i < len; i++ ) {
  6. var section = sections[i];
  7. var template = section.querySelector( 'script' );
  8. // strip leading whitespace so it isn't evaluated as code
  9. var text = ( template || section ).innerHTML;
  10. var leadingWs = text.match(/^\n?(\s*)/)[1].length,
  11. leadingTabs = text.match(/^\n?(\t*)/)[1].length;
  12. if( leadingTabs > 0 ) {
  13. text = text.replace( new RegExp('\\n?\\t{' + leadingTabs + '}','g'), '\n' );
  14. }
  15. else if( leadingWs > 1 ) {
  16. text = text.replace( new RegExp('\\n? {' + leadingWs + '}','g'), '\n' );
  17. }
  18. section.innerHTML = (new Showdown.converter()).makeHtml(text);
  19. }
  20. })();