Skip to content

Commit

Permalink
Use markly as markdown parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Moeki Kawakami committed Mar 4, 2024
1 parent 1652acd commit 7b9a8b7
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ mato.process("Hello!").render_html # "<p>HELLO!</p>\n"

### Markdown Filters

A markdown filter is a callable instance that takes a ``CommonMarker::Node`
A markdown filter is a callable instance that takes a ``Markly::Node`
and mutate it in the method. The return value is ignored.

For example:
Expand Down
20 changes: 11 additions & 9 deletions lib/mato/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
require_relative('./document')

require 'nokogiri'
require 'markly'

module Mato
class Config
# https://github.com/gjtorikian/commonmarker#parse-options
DEFAULT_MARKDOWN_PARSE_OPTIONS = %i[
DEFAULT
VALIDATE_UTF8
FOOTNOTES
DEFAULT_MARKDOWN_PARSE_OPTIONS = [
Markly::DEFAULT,
Markly::VALIDATE_UTF8,
Markly::FOOTNOTES,
Markly::UNSAFE,
].freeze

# https://github.com/gjtorikian/commonmarker#render-options
Expand Down Expand Up @@ -39,7 +41,7 @@ class Config
# @return [Array<Proc>]
attr_accessor :html_filters

# @return [Class<CommonMarker]
# @return [Class<Markly]
attr_accessor :markdown_parser

# @return [Cass<Nokogiri::HTML4::DocumentFragment>]
Expand All @@ -48,21 +50,21 @@ class Config
# @return [Class<Mato::Document>]
attr_accessor :document_factory

# @return [Array<Symbol>] CommonMarker's parse extensions
# @return [Array<Symbol>] Markly's parse extensions
attr_accessor :markdown_extensions

# @return [Array<Symbol>] CommonMarker's pars options
# @return [Array<Class>] Markly's pars options
attr_accessor :markdown_parse_options

# @return [Array<Symbol>] CommonMarker's HTML rendering options
# @return [Array<Symbol>] Commonmarker's HTML rendering options
attr_accessor :markdown_render_options

def initialize
@text_filters = []
@markdown_filters = []
@html_filters = []

@markdown_parser = CommonMarker
@markdown_parser = Markly
@html_parser = Nokogiri::HTML4::DocumentFragment

@document_factory = Document
Expand Down
6 changes: 3 additions & 3 deletions lib/mato/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def initialize(processor, content, flavor)
end

def run
# @type [CommonMarker::Node]
# @type [Markly::Node]
document = processor.parse_markdown(content)

convert_headings!(document)
Expand All @@ -45,14 +45,14 @@ def run
def convert_headings!(document)
document.walk.select do |node|
node.type == :text &&
node.sourcepos[:start_column] == 1 &&
node.source_position[:start_column] == 1 &&
node.parent.type == :paragraph &&
node.parent.parent.type == :document
end.reverse_each do |node|
replacement = node.string_content.gsub(/\A(#+)(?=\S)/, '\1 ')

if node.string_content != replacement
pos = node.sourcepos
pos = node.source_position
content_lines[pos[:start_line] - 1][(pos[:start_column] - 1)...pos[:end_column]] = replacement
end
end
Expand Down
11 changes: 7 additions & 4 deletions lib/mato/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class Processor

def initialize(config)
@config = config
config.markdown_parse_options.each do |options|
@flags = @flags ? @flags | options : options
end
end

# @param [String] input
Expand Down Expand Up @@ -43,15 +46,15 @@ def process(input)
end

# @param [String] text
# @return [CommonMarker::Node]
# @return [Markly::Node]
def parse_markdown(text)
config.markdown_parser.render_doc(text, config.markdown_parse_options, config.markdown_extensions)
config.markdown_parser.parse(text, flags: @flags, extensions: config.markdown_extensions)
end

# @param [CommonMarker::Node] markdown_node
# @param [Markly::Node] markdown_node
# @return [String]
def render_to_html(markdown_node)
markdown_node.to_html(config.markdown_render_options)
markdown_node.to_html(flags: @flags)
end

# @param [String] html
Expand Down

0 comments on commit 7b9a8b7

Please sign in to comment.