Last active
October 24, 2018 21:29
-
-
Save stevecheckoway/aa110671b8af5a090a5f38d1af6fd0a2 to your computer and use it in GitHub Desktop.
Simple Jekyll plugin to 1. parse `$…$` or `\(…\)` as inline math; 2. parse `$$…$$` as display math both inside and outside paragraphs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a (more) sane math parsing scheme. | |
require 'kramdown/parser/kramdown' | |
require 'kramdown/parser/kramdown/block_boundary' | |
module Kramdown | |
module Parser | |
class Skramdown < Kramdown::Parser::GFM | |
def initialize(source, options) | |
super | |
idx = @span_parsers.index(:inline_math) | |
@span_parsers[idx] = :sane_inline_math | |
@span_parsers.insert(idx, :sane_display_math) | |
end | |
# (?<!\\|\$) Isn't preceded by a backslash or a dollar sign | |
# \$ Dollar sign | |
# (?!\$) Isn't followed by a dollar sign | |
# (.*?) Nongreedy match | |
# (?<!\\|\$) Isn't preceded by a backslash or a dollar sign | |
# \$ Dollar sign | |
# (?!\$) Isn't followed by a dollar sign | |
# | Or | |
# (?<!\\) Not a backslash | |
# \\\( Backslash, open parenthesis | |
# (.*?) Nongreedy match | |
# (?<!\\) Isn't preceded by a backslash | |
# \\\) Backslash, close parenthesis | |
SANE_INLINE_MATH_START = /(?<!\\|\$)\$(?!\$)(.*?)(?<!\\|\$)\$(?!\$)|(?<!\\)\\\((.*?)(?<!\\)\\\)/m | |
def parse_sane_inline_math | |
start_line_number = @src.current_line_number | |
@src.pos += @src.matched_size | |
data = (@src[1] || @src[2]).strip | |
@tree.children << Element.new(:math, data, nil, :category => :span, :location => start_line_number) | |
end | |
define_parser(:sane_inline_math, SANE_INLINE_MATH_START, '\\$|\\\\\\(') | |
SANE_DISPLAY_MATH_START = /(?<!\\)\$\$(.*?)\$\$/m | |
def parse_sane_display_math | |
start_line_number = @src.current_line_number | |
@src.pos += @src.matched_size | |
data = (@src[1] || @src[2]).strip | |
@tree.children << Element.new(:math, data, nil, :category => :block, :location => start_line_number) | |
end | |
define_parser(:sane_display_math, SANE_DISPLAY_MATH_START, '\\$\\$') | |
end | |
end | |
end | |
# vim: set sw=2 sts=2 ts=8 et: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Files:
skramdown.rb
goes in_plugins
.katex.min.js
from KaTeX goes anywhere you want, I put it in akatex
directory.In my site's
Gemfile
, I haveIn my site's
_config.yml
, I haveIn my
_layouts/default.html
, I haveTo include the KaTeX CSS file in a page, I set
in the page's front matter.
After this is done, I noticed that the font size is too large and that display math inside paragraphs was respecting the paragraph's
text-indent
. These are easily fixed with the CSS (actually SASS)