Skip to content

Commit bd6d2ec

Browse files
committed
- Add support for [[Short Links]]
1 parent a7acad0 commit bd6d2ec

9 files changed

Lines changed: 46 additions & 16 deletions

File tree

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@
44
[![Build Status](https://github.com/DannyBen/madness/workflows/Test/badge.svg)](https://github.com/DannyBen/madness/actions?query=workflow%3ATest)
55
[![Maintainability](https://api.codeclimate.com/v1/badges/fa440dc4dbf895734d74/maintainability)](https://codeclimate.com/github/DannyBen/madness/maintainability)
66

7-
---
8-
## Differences from [original](https://github.com/DannyBen/madness)
9-
10-
### support for link format used by smart-notes apps like Obsidian.
11-
12-
Markdown based smart-notes apps frequently use a non-standard link format in the form of `[[Link]]` where Link is equally the link text and the name of the local markdown file to link to. `[[Link]]` would therefore translate to `[Link](./Link.md)`
13-
147
---
158

169
## Screenshots (click to zoom)
@@ -33,6 +26,7 @@ Markdown based smart-notes apps frequently use a non-standard link format in the
3326
* [Search](#search)
3427
* [Images and Static Files](#images-and-static-files)
3528
* [Automatic H1](#automatic-h1)
29+
* [Shortlinks](#shortlinks)
3630
* [Table of Contents Generation](#table-of-contents-generation)
3731
* [Hidden Directories](#hidden-directories)
3832
* [Controlling Sort Order](#controlling-sort-order)
@@ -77,6 +71,7 @@ searching for local, markdown based documentation directories.
7771
- Automatic generation of navigation sidebar.
7872
- Automatic generation of Table of Contents (site-wide and inline).
7973
- Can optionally show additional file types in the navigation menu (e.g. PDF files).
74+
- Optional support for `[[Short Link]]` syntax.
8075

8176
## Usage
8277

@@ -158,6 +153,9 @@ line_numbers: true
158153
# enable the copy to clipboard icon for code snippets
159154
copy_code: true
160155

156+
# convert [[Links]] to [Links](Links)
157+
shortlinks: false
158+
161159
# generate a table of contents file with this name, for example:
162160
# toc: Table of Contents
163161
toc: ~
@@ -183,9 +181,6 @@ expose_extensions: ~
183181
# exclude directories that match these regular expressions
184182
# note that this is an array
185183
exclude: ['^[a-z_\-0-9]+$']
186-
187-
# support for internal links in the form [[Link Title and Filename]]
188-
shortlinks: true
189184
```
190185
191186
For convenience, you can generate a template config file by running:
@@ -226,6 +221,13 @@ the path relative to the homepage:
226221
If your markdown document does not start with a level 1 heading, it
227222
will be automatically added based on the file name.
228223

224+
## Shortlinks
225+
226+
When the `shortlinks` option is enabled, you may use a shorthand syntax for
227+
specifying internal links, where `[[Anything]]` will be converted to
228+
`[Anything](Anything)`, which will then be rendered as an internal link to a
229+
file or a directory in the same directory as the file itself.
230+
229231
## Table of Contents Generation
230232

231233
### Site-wide

lib/madness/command_line.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,15 @@ def set_config(args)
7474
config.highlighter = false if args['--no-syntax']
7575
config.line_numbers = false if args['--no-line-numbers']
7676
config.copy_code = false if args['--no-copy-code']
77+
config.shortlinks = true if args['--shortlinks']
7778
config.open = true if args['--open']
7879

7980
config.theme = File.expand_path(args['--theme'], config.path) if args['--theme']
8081
end
8182

8283
# Generate index and toc, if requested by the user.
8384
def generate_stuff
84-
build_toc if config.toc
85+
build_toc if config.toc
8586
end
8687

8788
# Create config

lib/madness/docopt.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ Options:
5555
files.
5656
(Config option: auto_nav)
5757

58+
--shortlinks
59+
Enable support for [[Short Links]].
60+
(Config option: shortlinks)
61+
5862
--theme FOLDER
5963
Use a custom theme. FOLDER is either absolute or relative to the main
6064
documentation path.

lib/madness/document.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ def set_base_attributes_for_directory
6666
end
6767

6868
def markdown
69-
@markdown ||= File.read file
70-
@markdown = shortlinks(@markdown) if config.shortlinks
71-
@markdown
69+
@markdown ||= pre_process_markdown
7270
end
7371

74-
def shortlinks(raw)
75-
raw.gsub(/\[\[([^\]]+)\]\]/) { "[#{Regexp.last_match(1)}](./#{Regexp.last_match(1).to_href}.md)" }
72+
def pre_process_markdown
73+
result = File.read file
74+
result = evaluate_shortlinks result if config.shortlinks
75+
result
7676
end
7777

7878
def doc
@@ -119,6 +119,11 @@ def replace_toc_marker
119119
toc_marker.insert_after CommonMarker.render_doc("## Table of Contents").first_child
120120
end
121121

122+
# Replace [[link]] with [link](link)
123+
def evaluate_shortlinks(raw)
124+
raw.gsub(/\[\[([^\]]+)\]\]/) { |match| "[#{$1}](#{$1.to_href})" }
125+
end
126+
122127
# Returns a UL object containing the document table of contents
123128
def document_toc
124129
toc = []

lib/madness/templates/madness.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ line_numbers: true
2929
# enable the copy to clipboard icon for code snippets
3030
copy_code: true
3131

32+
# convert [[Links]] to [Links](Links)
33+
shortlinks: false
34+
3235
# generate a table of contents file with this name, for example:
3336
# toc: Table of Contents
3437
toc: ~

spec/fixtures/cli/help

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ Options:
5555
files.
5656
(Config option: auto_nav)
5757

58+
--shortlinks
59+
Enable support for [[Short Links]].
60+
(Config option: shortlinks)
61+
5862
--theme FOLDER
5963
Use a custom theme. FOLDER is either absolute or relative to the main
6064
documentation path.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This file contains a [[Shortlink]] to an internal file.
2+

spec/fixtures/toc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
1. [Double Escape](/Double%20Escape)
2828
1. [File](/File)
2929
1. [File-with-Dashes](/File-with-Dashes)
30+
1. [File with Shortlink](/File%20with%20Shortlink)
3031
1. [File with Spaces](/File%20with%20Spaces)
3132
1. [File with TOC](/File%20with%20TOC)
3233
1. [File without H1](/File%20without%20H1)

spec/madness/document_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,5 +276,13 @@
276276
expect(doc.content).not_to have_tag :h1
277277
end
278278
end
279+
280+
context "with shortlinks enabled" do
281+
it "evaluates shortlinks" do
282+
config.shortlinks = true
283+
doc = described_class.new "File with Shortlink"
284+
expect(doc.content).to have_tag(:a, with: { href: "Shortlink" }, text: "Shortlink")
285+
end
286+
end
279287
end
280288
end

0 commit comments

Comments
 (0)