yard-markdown-relative-links
A YARD plugin to convert relative links between Markdown files.
GitHub and YARD render Markdown files differently. In particular, relative links in Markdown files that work in GitHub don't work in YARD. For example, if you have [hello](FOO.md) in your README, YARD renders it as <a href="FOO.md">hello</a>, creating a broken link in your docs.
With this plugin enabled, you'll get <a href="file.FOO.html">hello</a> instead, which correctly links through to the rendered HTML file.
Features
- Converts relative Markdown links to YARD file references
-
Supports files in subdirectories — links from
docs/index.mdtogetting-started.mdcorrectly resolve todocs/getting-started.md - Preserves anchor/fragment links (e.g.,
file.md#section) - Falls back to basename matching for simple cases
- No
Nokogiriruntime dependency (stdlib-only link rewriting)
Installation
Add this line to your application's Gemfile:
gem 'yard-markdown-relative-links'And then execute:
bundle installOr install it yourself as:
gem install yard-markdown-relative-linksUsage
Add this line to your application's .yardopts:
--plugin markdown-relative-links
Important: the YARD plugin key is markdown-relative-links.
Do not use --plugin relative_markdown_links, --plugin yard-relative_markdown_links, or --plugin yard-markdown-relative-links.
You'll also need to make sure your Markdown files are processed by YARD. To include all Markdown files in your project, add the following lines to the end of your application's .yardopts:
-
**/*.md
Example .yardopts
--markup markdown
--plugin markdown-relative-links
--readme README.md
-
docs/*.md
lib/**/*.rb
How It Works
When YARD processes your Markdown files, this plugin intercepts the HTML output and converts relative links to YARD's {file:} syntax.
The plugin resolves links in three ways:
-
Exact match — If the link path exactly matches a file in YARD's file list (e.g.,
docs/index.md), it's used directly. -
Relative to current file — If processing
docs/index.mdand it contains a link togetting-started.md, the plugin resolves this relative to the current file's directory, findingdocs/getting-started.md. -
Basename fallback — If there's exactly one file with the matching basename, it's used (e.g.,
getting-started.mdmatchesdocs/getting-started.mdif that's the only file with that name).
Background
Origins
This gem is a replacement for the original yard-relative_markdown_links gem created by Andrew Haines. The original gem was archived in February 2026.
The original gem solved an important problem: relative links between Markdown files that work on GitHub don't work in YARD-generated documentation. However, it had a limitation — it only matched links against exact file paths in YARD's file list, which meant links between files in subdirectories didn't work properly.
For example, if you had documentation in a docs/ folder:
docs/
├── index.md # contains [Getting Started](getting-started.md)
└── getting-started.md
The link in index.md would work on GitHub but not in YARD, because the original gem looked for getting-started.md in the file list, but YARD registered it as docs/getting-started.md.
Comparison with the Original Gem
| Feature | Original | This Gem |
|---|---|---|
| Convert exact path matches | ✅ | ✅ |
Preserve fragment/anchors (#section) |
✅ | ✅ |
| Skip absolute URLs | ✅ | ✅ |
| RDoc filename mapping | ✅ | ✅ |
| Subdirectory support | ❌ | ✅ |
Parent directory (..) resolution |
❌ | ✅ |
| Basename fallback matching | ❌ | ✅ |
| Malformed URI handling | ❌ | ✅ |
Key Improvements
-
Subdirectory support — When processing
docs/index.md, a link togetting-started.mdis resolved relative to the current file's directory, correctly findingdocs/getting-started.md. -
Parent directory resolution — Links like
../other-file.mdare normalized and resolved correctly. -
Basename fallback — If there's exactly one file with a matching basename, it's used even without the full path.
-
Error handling — Malformed URIs are gracefully skipped instead of raising exceptions.
Development
Run tests:
bundle exec rake testRun linting:
bundle exec rake lintLicense
Released under the MIT License.