Skip to content

Instantly share code, notes, and snippets.

@brynne8
Last active December 19, 2021 08:32
Show Gist options
  • Save brynne8/2553a9074fa7508da284ca844020719e to your computer and use it in GitHub Desktop.
Save brynne8/2553a9074fa7508da284ca844020719e to your computer and use it in GitHub Desktop.
Part of a Markdown lexer
local punct_space = lexer.punct + lexer.space
-- Handles flanking delimiters as described in
-- https://github.github.com/gfm/#emphasis-and-strong-emphasis in the cases
-- where simple delimited ranges are not sufficient.
local function flanked_range(s, not_inword)
local fl_char = lexer.any - s - lexer.space
local left_fl = lpeg.B(punct_space - s) * s * #fl_char +
s * #(fl_char - lexer.punct)
local right_fl = lpeg.B(lexer.punct) * s * #(punct_space - s) +
lpeg.B(fl_char) * s
return left_fl * (lexer.any - (not_inword and s * #punct_space or s))^0 *
right_fl
end
lex:add_rule('strong',
token('strong', flanked_range('**') +
(lpeg.B(punct_space) + #lexer.starts_line('_')) *
flanked_range('__', true) * #(punct_space + -1)))
lex:add_style('strong', 'bold')
lex:add_rule('em',
token('em', flanked_range('*') +
(lpeg.B(punct_space) + #lexer.starts_line('_')) *
flanked_range('_', true) * #(punct_space + -1)))
lex:add_style('em', 'italics')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment