description prismjs.rb

Usage in Jekyll

First, include prism.js in your header. Then save the code below and save it as prismjs.rb in the folder _plugins in your Jekyll root directory.

Quick example:


{% prism cpp %}
int main() {}
{% endprism %}

Code



# Purpose:	Jekyll Plugin to prepare markdown for syntax highlighting with prism.js
# License:	MIT
# Note:		This script transforms code to escaped html entities before it puts it back, so Kramdown does not get confused.
# Author:   Domeniko Gentner <contact@tuxstash.de>
require 'cgi'

module Jekyll
	module Tags
		class Prism < Liquid::Block
			def initialize(tag_name, text, tokens)
				@arg = text.strip
				super
			end

			def render(context)
				output = super(context)
				output = CGI.escapeHTML(output);
				"<pre class=\"language-#{@arg} line-numbers card-panel z-depth-3 \"><code>#{output}</code></pre>"
			end
		end
	end
end

Liquid::Template.register_tag('prism', Jekyll::Tags::Prism)

tweet gist  •  August 24 2019