structuredmerge Logo by GitHub

โ˜ฏ๏ธ Commonmarker::Merge

Version Ruby Users Forum GitHub tag (latest SemVer) License: AGPL-3.0-only OR PolyForm-Small-Business-1.0.0 Total downloads CI Current

if ci_badges.map(&:color).detect { it != "green"} โ˜๏ธ let me know on Discord or RubyForum, as I may have missed the notification.


if ci_badges.map(&:color).all? { it == "green"} ๐Ÿ‘‡๏ธ send money so I can do more of this. FLOSS maintenance is now my full-time job.

Sponsor Me on Github Liberapay Goal Progress Donate on PayPal Buy me a coffee Donate at ko-fi.com

๐Ÿ‘ฃ How will this project approach the September 2025 hostile takeover of RubyGems? ๐Ÿš‘๏ธ

I've summarized my thoughts in this blog post.

๐ŸŒป Synopsis Galtzo FLOSS Logo by Aboling0, CC BY-SA 4.0 ruby-lang Logo, Yukihiro Matsumoto, Ruby Visual Identity Team, CC BY-SA 2.5

commonmarker-merge is a thin wrapper around markdown-merge that provides:

  • Hard dependency on Commonmarker - Ensures the Comrak (Rust) parser is installed
  • Commonmarker-specific defaults - Freeze token: "commonmarker-merge", inner_merge_code_blocks: false
  • CommonMarker parse options - Pass options via the options: parameter

Features (via markdown-merge)

  • Smart element matching - Headings, paragraphs, lists, code blocks, and other block elements are matched by their structural signatures
  • Fuzzy table matching - Tables are matched using a multi-factor scoring algorithm that considers header similarity, first column (row labels), content overlap, and position
  • Freeze blocks - Mark sections with HTML comments to preserve them during merges
  • Configurable merge strategies - Choose whether template or destination wins for conflicts, or use a Hash for per-node-type preferences with node_splitter (see ast-merge docs)
  • Conservative removal mode - remove_template_missing_nodes: true removes top-level destination-only structural blocks while preserving standalone HTML comment-only fragments, link reference definitions, freeze blocks, and stable separator blank lines around preserved standalone fragments
  • Type normalization - Canonical node types work across all markdown backends
  • Full CommonMarker support - Works with all CommonMark and GitHub Flavored Markdown extensions

Removal Mode Scope

remove_template_missing_nodes: true in commonmarker-merge follows the shared markdown-merge full-document contract:

  • removes top-level destination-only structural blocks
  • preserves standalone HTML comment-only fragments, link reference definitions, and freeze blocks
  • preserves one separator blank line when removed structural content collapses around a kept standalone HTML comment fragment
  • does not yet define generic inline-comment promotion or recursive/nested section-removal semantics

Section-local replace_mode / partial-template behavior still follows its own conservative Markdown rules and should not be assumed to inherit the same recursive removal contract as full-document smart merge.

๐Ÿ’ก Info you can shake a stick at

Tokens to Remember Gem name Gem namespace
Works with MRI Ruby 4 Ruby current Compat
Support & Community Join Me on Daily.dev's RubyFriends Get help from RubyForum Live Chat on Discord Get help from me on Upwork Get help from me on Codementor
Source Source on GitLab.com Source on CodeBerg.org Source on GitHub.com The best SHA: dQw4w9WgXcQ!
Documentation Current release on RubyDoc.info YARD on Galtzo.com Maintainer Blog GitLab Wiki GitHub Wiki
Compliance License: AGPL-3.0-only OR PolyForm-Small-Business-1.0.0 Apache license compatibility: Category X ๐Ÿ“„ilo-declaration-img Security Policy Contributor Covenant 2.1 SemVer 2.0.0
Style Enforced Code Style Linter Keep-A-Changelog 1.0.0 Gitmoji Commits Compatibility appraised by: appraisal2
Maintainer ๐ŸŽ–๏ธ Follow Me on LinkedIn Follow Me on Ruby.Social Follow Me on Bluesky Contact Maintainer My technical writing
... ๐Ÿ’– Find Me on WellFound: Find Me on CrunchBase My LinkTree More About Me ๐ŸงŠ ๐Ÿ™ ๐Ÿ›– ๐Ÿงช

Compatibility

Compatible with MRI Ruby 4.0.0+, and concordant releases of JRuby, and TruffleRuby. CI workflows and Appraisals are generated for MRI Ruby 4.0.0+. This test floor is configured by ruby.test_minimum in .kettle-jem.yml and may be higher than the gem's runtime compatibility floor when legacy Rubies are not practical for the current toolchain.

kettle-dev Logo by Aboling0, CC BY-SA 4.0

The amazing test matrix is powered by the kettle-dev stack.

How kettle-dev manages complexity in tests
Gem Source Role Total downloads
appraisal2 GitHub multi-dependency Appraisal matrix generation Total downloads for appraisal2
appraisal2-rubocop GitHub RuboCop Appraisal generator integration Total downloads for appraisal2-rubocop
kettle-dev GitHub development, release, and CI workflow tooling Total downloads for kettle-dev
kettle-jem GitHub Appraisals & CI workflow templates Total downloads for kettle-jem
kettle-soup-cover GitHub SimpleCov coverage policy and reporting Total downloads for kettle-soup-cover
kettle-test GitHub standard test runner and coverage harness Total downloads for kettle-test
rubocop-lts GitHub Ruby-version-aware linting Total downloads for rubocop-lts
turbo_tests2 GitHub parallel test execution Total downloads for turbotests2

โœจ Installation

Install the gem and add to the application's Gemfile by executing:

bundle add commonmarker-merge

If bundler is not being used to manage dependencies, install the gem by executing:

gem install commonmarker-merge

โš™๏ธ Configuration

Freeze Blocks

Freeze blocks prevent sections from being modified during merges. They are marked with HTML comments that are invisible when the Markdown is rendered:

<!-- commonmarker-merge:freeze -->
## This Section Is Protected

Any content here will be preserved exactly as-is during merges.
The merge tool will not modify, replace, or remove this content.

<!-- commonmarker-merge:unfreeze -->

You can add an optional reason to document why a section is frozen:

<!-- commonmarker-merge:freeze Manual TOC - do not auto-generate -->
## Table of Contents
- [Installation](#installation)
- [Usage](#usage)
<!-- commonmarker-merge:unfreeze -->

Custom Freeze Token

Use a custom freeze token if you need to avoid conflicts with other tools:

merger = Commonmarker::Merge::SmartMerger.new(
  template,
  destination,
  freeze_token: "my-project",
)
# Now looks for: <!-- my-project:freeze --> and <!-- my-project:unfreeze -->

Merge Preferences

Control how conflicts between template and destination are resolved:

# Preserve destination customizations (default)
merger = Commonmarker::Merge::SmartMerger.new(
  template,
  destination,
  preference: :destination,
)

# Apply template updates (overwrite destination)
merger = Commonmarker::Merge::SmartMerger.new(
  template,
  destination,
  preference: :template,
)

# Add new sections from template that don't exist in destination
merger = Commonmarker::Merge::SmartMerger.new(
  template,
  destination,
  add_template_only_nodes: true,
)

Debug Logging

Enable debug logging to see merge decisions:

export COMMONMARKER_MERGE_DEBUG=1

Table Match Refiner

When tables don't match by exact signature (identical headers), the TableMatchRefiner uses fuzzy matching to pair tables that have:

  • Similar headers (e.g., "Value" vs "Values")
  • Similar first column content (row labels)
  • Similar overall structure and content
# Enable table fuzzy matching with custom threshold
merger = Commonmarker::Merge::SmartMerger.new(
  template,
  destination,
  match_refiners: [
    Commonmarker::Merge::TableMatchRefiner.new(threshold: 0.6),
  ],
)

TableMatchAlgorithm Weights

The TableMatchAlgorithm uses a multi-factor scoring system with configurable weights:

Factor Default Weight Description
header_match 0.25 Percentage of matching header cells (Levenshtein similarity)
first_column 0.20 Percentage of matching first column cells
row_content 0.25 Average match percentage for rows with matching first column
total_cells 0.15 Overall cell matching percentage
position 0.15 Position distance (closer tables score higher)
# Custom weights for specific use cases
refiner = Commonmarker::Merge::TableMatchRefiner.new(
  threshold: 0.5,
  algorithm_options: {
    weights: {
      header_match: 0.4,  # Prioritize header matching
      first_column: 0.2,
      row_content: 0.2,
      total_cells: 0.1,
      position: 0.1,
    },
  },
)

๐Ÿ”ง Basic Usage

Merging Two Markdown Files

require "commonmarker/merge"

template_content = File.read("template.md")
dest_content = File.read("destination.md")

merger = Commonmarker::Merge::SmartMerger.new(template_content, dest_content)
result = merger.merge

if result.success?
  File.write("destination.md", result.content)
  puts "Merged successfully!"
  puts "  - Nodes added: #{result.nodes_added}"
  puts "  - Nodes modified: #{result.nodes_modified}"
  puts "  - Frozen blocks preserved: #{result.frozen_count}"
else
  puts "Merge had conflicts:"
  result.conflicts.each do |conflict|
    puts "  - #{conflict[:location]}: #{conflict[:reason]}"
  end
end

Analyzing a Markdown File

require "commonmarker/merge"

source = File.read("README.md")
analysis = Commonmarker::Merge::FileAnalysis.new(source)

# Iterate over all block elements
analysis.statements.each do |node|
  case node
  when Commonmarker::Merge::FreezeNode
    puts "Freeze block: lines #{node.start_line}-#{node.end_line}"
    puts "  Reason: #{node.reason}" if node.reason
  else
    sig = analysis.generate_signature(node)
    puts "#{node.type}: #{sig.inspect}"
  end
end

# Get just the freeze blocks
analysis.freeze_blocks.each do |freeze_node|
  puts "Protected: #{freeze_node.content[0..50]}..."
end

Custom Signature Generator

Override how elements are matched between files:

# Match headings only by level, ignoring content
custom_sig = ->(node) {
  if node.respond_to?(:type) && node.type == :heading
    [:heading, node.header_level]  # Match any h1 to any h1, etc.
  else
    node  # Fall through to default signature
  end
}

merger = Commonmarker::Merge::SmartMerger.new(
  template,
  destination,
  signature_generator: custom_sig,
)

Fuzzy Table Matching

When merging documents with tables that have been renamed or restructured, use the TableMatchRefiner to find the best matches:

require "commonmarker/merge"

template = <<~MD
  # API Reference

  | Endpoint | Method | Description |
  |----------|--------|-------------|
  | /users   | GET    | List users  |
  | /users   | POST   | Create user |
MD

destination = <<~MD
  # API Reference

  | API Endpoint | HTTP Method | Descriptions |
  |--------------|-------------|--------------|
  | /users       | GET         | List users   |
  | /posts       | GET         | List posts   |
MD

# Default merge won't match the tables (headers differ)
# Use TableMatchRefiner to enable fuzzy matching
merger = Commonmarker::Merge::SmartMerger.new(
  template,
  destination,
  match_refiners: [
    Commonmarker::Merge::TableMatchRefiner.new(threshold: 0.5),
  ],
)
result = merger.merge

# Tables are now matched despite header differences
# ("Endpoint" ~ "API Endpoint", "Method" ~ "HTTP Method", etc.)

๐Ÿ” Security

See SECURITY.md.

๐Ÿค Contributing

If you need some ideas of where to help, you could work on adding more code coverage, or if it is already ๐Ÿ’ฏ (see below) check issues or PRs, or use the gem and think about how it could be better.

We Keep A Changelog so if you make changes, remember to update it.

See CONTRIBUTING.md for more detailed instructions.

Code Coverage

Coverage service badges

๐Ÿ“Œ Versioning

This library follows Semantic Versioning 2.0.0 for its public API where practical. For most applications, prefer the Pessimistic Version Constraint with two digits of precision.

For example:

spec.add_dependency("commonmarker-merge", "~> 7.0")
๐Ÿ“Œ Is "Platform Support" part of the public API? More details inside.

Dropping support for a platform can be a breaking change for affected users. If a release changes supported platforms, it should be called out clearly in the changelog and versioned with that impact in mind.

To get a better understanding of how SemVer is intended to work over a project's lifetime, read this article from the creator of SemVer:

See CHANGELOG.md for a list of releases.

๐Ÿ“„ License

The gem is available under the following licenses: AGPL-3.0-only, PolyForm-Small-Business-1.0.0. See LICENSE.md for details.

If none of the available licenses suit your use case, please contact us to discuss a custom commercial license.