Skip to content

Commit

Permalink
resolves asciidoctor#21 load highlight.js from a CDN
Browse files Browse the repository at this point in the history
  • Loading branch information
ggrossetie committed Jan 22, 2020
1 parent 7e53bd9 commit 470fc6d
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 3 deletions.
2 changes: 1 addition & 1 deletion asciidoctor-revealjs.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Gem::Specification.new do |s|
Dir['**/*']
end
# TODO should we still package template files now that they are built into ruby?
s.files = files.grep %r/^(?:(?:examples|lib|templates)\/.+|Gemfile|Rakefile|(?:CHANGELOG|LICENSE|README)\.adoc|#{s.name}\.gemspec)$/
s.files = files.grep %r/^(?:(?:examples|lib|templates|assets)\/.+|Gemfile|Rakefile|(?:CHANGELOG|LICENSE|README)\.adoc|#{s.name}\.gemspec)$/

s.executables = ['asciidoctor-revealjs']
s.extra_rdoc_files = Dir['README.adoc', 'LICENSE.adoc', 'HACKING.adoc']
Expand Down
76 changes: 76 additions & 0 deletions assets/plugin-highlight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* DO NOT MANUALLY EDIT THIS FILE */
/* this file was copied-pasted from https://github.com/hakimel/reveal.js/blob/3.7.0/plugin/highlight/highlight.js */
/* please note that the bundled highlight.js code was removed so we can use a more recent version */
(function() {
// Function to perform a better "data-trim" on code snippets
// Will slice an indentation amount on each line of the snippet (amount based on the line having the lowest indentation length)
function betterTrim(snippetEl) {
// Helper functions
function trimLeft(val) {
// Adapted from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim#Polyfill
return val.replace(/^[\s\uFEFF\xA0]+/g, '');
}
function trimLineBreaks(input) {
var lines = input.split('\n');

// Trim line-breaks from the beginning
for (var i = 0; i < lines.length; i++) {
if (lines[i].trim() === '') {
lines.splice(i--, 1);
} else break;
}

// Trim line-breaks from the end
for (var i = lines.length-1; i >= 0; i--) {
if (lines[i].trim() === '') {
lines.splice(i, 1);
} else break;
}

return lines.join('\n');
}

// Main function for betterTrim()
return (function(snippetEl) {
var content = trimLineBreaks(snippetEl.innerHTML);
var lines = content.split('\n');
// Calculate the minimum amount to remove on each line start of the snippet (can be 0)
var pad = lines.reduce(function(acc, line) {
if (line.length > 0 && trimLeft(line).length > 0 && acc > line.length - trimLeft(line).length) {
return line.length - trimLeft(line).length;
}
return acc;
}, Number.POSITIVE_INFINITY);
// Slice each line with this amount
return lines.map(function(line, index) {
return line.slice(pad);
})
.join('\n');
})(snippetEl);
}

if( typeof window.addEventListener === 'function' ) {
var hljs_nodes = document.querySelectorAll( 'pre code' );

for( var i = 0, len = hljs_nodes.length; i < len; i++ ) {
var element = hljs_nodes[i];

// trim whitespace if data-trim attribute is present
if( element.hasAttribute( 'data-trim' ) && typeof element.innerHTML.trim === 'function' ) {
element.innerHTML = betterTrim(element);
}

// Now escape html unless prevented by author
if( ! element.hasAttribute( 'data-noescape' )) {
element.innerHTML = element.innerHTML.replace(/</g,"&lt;").replace(/>/g,"&gt;");
}

// re-highlight when focus is lost (for edited code)
element.addEventListener( 'focusout', function( event ) {
hljs.highlightBlock( event.currentTarget );
}, false );
}
}
})();

hljs.initHighlightingOnLoad();
18 changes: 17 additions & 1 deletion lib/asciidoctor-revealjs/highlightjs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module SyntaxHighlighter
class HighlightJsAdapter < Asciidoctor::SyntaxHighlighter::Base
register_for 'highlightjs', 'highlight.js'

HIGHLIGHT_JS_VERSION = '9.18.0'

def initialize *args
super
@name = @pre_class = 'highlightjs'
Expand Down Expand Up @@ -33,7 +35,21 @@ def docinfo location, doc, opts
else
theme_href = "#{revealjsdir}/lib/css/zenburn.css"
end
%(<link rel="stylesheet" href="#{theme_href}"#{opts[:self_closing_tag_slash]}>)

unless (asset_uri_scheme = (doc.attr 'asset-uri-scheme', 'https')).empty?
asset_uri_scheme = %(#{asset_uri_scheme}:)
end
# use cdn.jsdelivr.net instead of cdnjs.com because it contains the latest version of highlight.js with additional languages
# please note that the bundled version of highlight.js in reveal.js contains 175 languages
# the prebuilt version of highlight.js hosted on cdn.jsdelivr.net contains only 34 commonly used languages.
# if you want additional languages, use "highlightjs-languages".
# QUESTION: should we also provide a bundled version of highlight.js with all the languages included?
cdn_base = %(#{asset_uri_scheme}//cdn.jsdelivr.net)
plugin_highlight_path = File.join(File.dirname(__FILE__), "../../assets/plugin-highlight.js")
base_url = doc.attr 'highlightjsdir', %(#{cdn_base}/gh/highlightjs/cdn-release@#{HIGHLIGHT_JS_VERSION}/build)
%(<link rel="stylesheet" href="#{theme_href}"#{opts[:self_closing_tag_slash]}>
<script src="#{base_url}/highlight.min.js"></script>
#{(doc.attr? 'highlightjs-languages') ? ((doc.attr 'highlightjs-languages').split ',').map {|lang| %[<script src="#{base_url}/languages/#{lang.lstrip}.min.js"></script>\n] }.join : ''}<script>#{File.read(plugin_highlight_path)}</script>)
end
end
end
Expand Down
1 change: 0 additions & 1 deletion templates/document.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,6 @@ html lang=(attr :lang, 'en' unless attr? :nolang)
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: '#{revealjsdir}/lib/js/classList.js', condition: function() { return !document.body.classList; } },
#{(document.attr? 'source-highlighter', 'highlightjs') ? "{ src: '#{revealjsdir}/plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }," : nil}
#{(attr? 'revealjs_plugin_zoom', 'disabled') ? "" : "{ src: '#{revealjsdir}/plugin/zoom-js/zoom.js', async: true }," }
#{(attr? 'revealjs_plugin_notes', 'disabled') ? "" : "{ src: '#{revealjsdir}/plugin/notes/notes.js', async: true }," }
#{(attr? 'revealjs_plugin_marked', 'enabled') ? "{ src: '#{revealjsdir}/plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } }," : "" }
Expand Down

0 comments on commit 470fc6d

Please sign in to comment.