Skip to content

Commit

Permalink
Merge pull request #31 from bitjourney/correspond_to_commonmarker_v1
Browse files Browse the repository at this point in the history
Correspond to commonmarker v1
  • Loading branch information
moekiorg authored May 7, 2024
2 parents 0325954 + e935903 commit ea92cdd
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 52 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/reviewdog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Use ruby
uses: ruby/setup-ruby@v1
with:
bundler-cache: true
ruby-version: '3.0'
ruby-version: '3.1'

- name: rubocop
uses: reviewdog/action-rubocop@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ruby_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby-version: ['2.7', '3.0', '3.1']
ruby-version: ["3.1", "3.2", "3.3"]
steps:
- uses: actions/checkout@v3
- name: Set up Ruby
Expand Down
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require:
- rubocop-performance

AllCops:
TargetRubyVersion: 2.7
TargetRubyVersion: 3.1
DisplayCopNames: true
Exclude:
- bin/**/*
Expand Down
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 ``Commonmarker::Node`
and mutate it in the method. The return value is ignored.

For example:
Expand Down
56 changes: 20 additions & 36 deletions lib/mato/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,21 @@

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

# https://github.com/gjtorikian/commonmarker#render-options
DEFAULT_MARKDOWN_RENDER_OPTIONS = [
:DEFAULT,
:HARDBREAKS, # convert "\n" as <br/>
:TABLE_PREFER_STYLE_ATTRIBUTES,
:UNSAFE,
# :SOURCEPOS, // TODO: enable it after assertions are supported
].freeze

# https://github.com/github/cmark/tree/master/extensions
DEFAULT_MARKDOWN_EXTENSIONS = %i[
table
strikethrough
autolink
tagfilter
].freeze
DEFAULT_MARKDOWN_OPTIONS = {
render: {
hardbreaks: true,
unsafe: true,
},
extension: {
table: true,
strikethrough: true,
autolink: true,
tagfilter: true,
tasklist: false,
shortcodes: false,
footnotes: true,
},
}.freeze

# @return [Array<Proc>]
attr_accessor :text_filters
Expand All @@ -39,7 +31,7 @@ class Config
# @return [Array<Proc>]
attr_accessor :html_filters

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

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

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

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

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

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

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

@document_factory = Document

@markdown_extensions = DEFAULT_MARKDOWN_EXTENSIONS
@markdown_parse_options = DEFAULT_MARKDOWN_PARSE_OPTIONS
@markdown_render_options = DEFAULT_MARKDOWN_RENDER_OPTIONS
@markdown_options = DEFAULT_MARKDOWN_OPTIONS
end

# @param [Proc] block
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 [Commonmarker::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
8 changes: 4 additions & 4 deletions lib/mato/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ def process(input)
end

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

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

# @param [String] html
Expand Down
4 changes: 2 additions & 2 deletions mato.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.required_ruby_version = ">= 2.7"
spec.required_ruby_version = "~> 3.1"

spec.add_runtime_dependency "commonmarker", ">= 0.18.1"
spec.add_runtime_dependency "commonmarker", "~> 1.0"
spec.add_runtime_dependency "nokogiri", ">= 1.12"
spec.add_runtime_dependency "rouge", ">= 3.0.0"
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
require 'active_support'
require 'rr'

class MyTest < MiniTest::Test
class MyTest < Minitest::Test
include Minitest::PowerAssert::Assertions

def assert_html_eq(actual, expected)
Expand Down

0 comments on commit ea92cdd

Please sign in to comment.