Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight Mint code in Markdown content. #677

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/parser/top_level.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,43 @@ module Mint
parser.ast
end

def parse_any : Nil
many do
oneof do
module_definition ||
type_definition ||
html_attribute ||
interpolation ||
css_font_face ||
css_keyframes ||
component ||
constant ||
property ||
operator ||
provider ||
css_node ||
function ||
comment ||
connect ||
locale ||
routes ||
route ||
state ||
store ||
style ||
suite ||
test ||
type ||
get ||
use ||
statement ||
case_branch ||
destructuring ||
expression
end
end
end

def parse : Nil
# Comment needs to be last since other comments are parsed with the
# entities.
Expand Down
5 changes: 4 additions & 1 deletion src/semantic_tokenizer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,13 @@ module Mint
getter tokens = [] of Token

def self.tokenize(ast : Ast)
parts = [] of String | Tuple(SemanticTokenizer::TokenType, String)

return parts if ast.nodes.empty?

tokenizer = self.new
tokenizer.tokenize(ast)

parts = [] of String | Tuple(SemanticTokenizer::TokenType, String)
contents = ast.nodes.first.file.contents
position = 0

Expand Down
25 changes: 24 additions & 1 deletion src/utils/markd_vdom_renderer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,30 @@ module Mint

tag("pre")
tag("code", code_attributes)
@io << '`' << node.text.gsub('`', "\\`").strip << '`'

if language == "mint"
parser = Parser.new(node.text, "source.mint")
parser.parse_any

parts =
SemanticTokenizer.tokenize(parser.ast)

parts.each do |item|
case item
in String
@io << '`' << item.gsub('`', "\\`") << '`'
in Tuple(SemanticTokenizer::TokenType, String)
tag("span", {"className" => item[0].to_s.underscore})
@io << '`' << item[1].gsub('`', "\\`") << '`'
tag_end
end

@io << ','
end
else
@io << '`' << node.text.gsub('`', "\\`").strip << '`'
end
Comment on lines +73 to +94
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest extracting this into a dedicated method:

def inline_code(io, code)
  io << '`' << code.gsub('`', "\\`") << '`'
end
Suggested change
if language == "mint"
parser = Parser.new(node.text, "source.mint")
parser.parse_any
parts =
SemanticTokenizer.tokenize(parser.ast)
parts.each do |item|
case item
in String
@io << '`' << item.gsub('`', "\\`") << '`'
in Tuple(SemanticTokenizer::TokenType, String)
tag("span", {"className" => item[0].to_s.underscore})
@io << '`' << item[1].gsub('`', "\\`") << '`'
tag_end
end
@io << ','
end
else
@io << '`' << node.text.gsub('`', "\\`").strip << '`'
end
if language == "mint"
parser = Parser.new(node.text, "source.mint")
parser.parse_any
parts =
SemanticTokenizer.tokenize(parser.ast)
parts.each do |item|
case item
in String
inline_code(@io, item)
in Tuple(SemanticTokenizer::TokenType, String)
tag("span", {"className" => item[0].to_s.underscore})
inline_code(@io, item[1])
tag_end
end
@io << ','
end
else
inline_code(@io, node.text.strip)
end


tag_end
tag_end(node)
end
Expand Down