From f9a9b90ed7b782fe1bdcf892f5cc2b10a5ea27ae Mon Sep 17 00:00:00 2001 From: simonzack Date: Wed, 28 Jan 2015 18:00:35 +1100 Subject: [PATCH] enable mathjax in live preview --- lib/gollum/app.rb | 12 +++++++--- .../gollum/livepreview/js/livepreview.js | 12 ++++++---- .../livepreview.mustache} | 24 ++++++++++++++++++- lib/gollum/views/livepreview.rb | 13 ++++++++++ 4 files changed, 53 insertions(+), 8 deletions(-) rename lib/gollum/{public/gollum/livepreview/index.html => templates/livepreview.mustache} (73%) create mode 100644 lib/gollum/views/livepreview.rb diff --git a/lib/gollum/app.rb b/lib/gollum/app.rb index 05057df6..315f0cd5 100644 --- a/lib/gollum/app.rb +++ b/lib/gollum/app.rb @@ -28,7 +28,7 @@ class String self # Do not transliterate utf-8 url's unless using Grit end end - + # _Header => header which causes errors def to_url return nil if self.nil? @@ -148,7 +148,7 @@ module Precious @allow_uploads = wiki.allow_uploads if page = wikip.page if wiki.live_preview && page.format.to_s.include?('markdown') && supported_useragent?(request.user_agent) - live_preview_url = '/livepreview/index.html?page=' + encodeURIComponent(@name) + live_preview_url = '/livepreview/?page=' + encodeURIComponent(@name) if @path live_preview_url << '&path=' + encodeURIComponent(@path) end @@ -362,6 +362,12 @@ module Precious mustache :page end + get '/livepreview/' do + wiki = wiki_new + @mathjax = wiki.mathjax + mustache :livepreview, { :layout => false } + end + get '/history/*' do @page = wiki_page(params[:splat].first).page @page_num = [params[:page].to_i, 1].max @@ -379,7 +385,7 @@ module Precious @versions = @wiki.latest_changes({:max_count => max_count}) mustache :latest_changes end - + post '/compare/*' do @file = encodeURIComponent(params[:splat].first) @versions = params[:versions] || [] diff --git a/lib/gollum/public/gollum/livepreview/js/livepreview.js b/lib/gollum/public/gollum/livepreview/js/livepreview.js index 15fe092c..e8e93b13 100644 --- a/lib/gollum/public/gollum/livepreview/js/livepreview.js +++ b/lib/gollum/public/gollum/livepreview/js/livepreview.js @@ -311,6 +311,10 @@ var makePreviewHtml = function () { // preview.innerHTML = text; // this doesn't work on IE. previewSet( text ); + if (win.MathJax) { + win.MathJax.Hub.Queue(['Typeset', win.MathJax.Hub, 'contentframe']); + } + // highlight code blocks. var codeElements = preview.getElementsByTagName( 'pre' ); var codeElementsLength = codeElements.length; @@ -370,19 +374,19 @@ var applyTimeout = function () { ~-1 == false; !~-1 == true; */ if ( !~ location.host.indexOf( 'github.com' ) ) { - + // returns unescaped key with leading slashes removed function key_no_leading_slash( key ) { return unescape( $.key( key ) || '' ).replace( /^\/+/, '' ); } - + // ensure leading / is removed from path and that it ends with / var path = key_no_leading_slash( 'path' ); // don't append '/' if path is empty from removing leading slash if ( path !== '' && path.charAt( path.length - 1 ) !== '/' ) { - path += '/'; + path += '/'; } - + jQuery.ajax( { type: 'GET', url: baseUrl + '/data/' + path + key_no_leading_slash( 'page' ), diff --git a/lib/gollum/public/gollum/livepreview/index.html b/lib/gollum/templates/livepreview.mustache similarity index 73% rename from lib/gollum/public/gollum/livepreview/index.html rename to lib/gollum/templates/livepreview.mustache index a7eef1e7..a290224b 100644 --- a/lib/gollum/public/gollum/livepreview/index.html +++ b/lib/gollum/templates/livepreview.mustache @@ -9,7 +9,7 @@
- +