Skip to content

Commit

Permalink
Add processing for url fragments to vimwiki_link
Browse files Browse the repository at this point in the history
  • Loading branch information
djeremiah committed Feb 21, 2020
1 parent 0785a2b commit b67e181
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/vimwiki_markdown/vimwiki_link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@

module VimwikiMarkdown
class VimwikiLink
MARKDOWN_LINK_REGEX = /\[(?<title>.*)\]\((?<uri>.*)\)/
MARKDOWN_LINK_REGEX = /\[(?<title>.*)\]\((?<uri>(?:(?!#).)*)(?<fragment>(?:#)?.*)\)/

attr_reader :title, :uri, :source_markdown_directory, :markdown_extension, :root_path
attr_reader :title, :uri, :fragment, :source_markdown_directory, :markdown_extension, :root_path

def initialize(markdown_link, source_markdown_filepath, markdown_extension, root_path)
@title = markdown_link.match(MARKDOWN_LINK_REGEX)[:title]
@uri = markdown_link.match(MARKDOWN_LINK_REGEX)[:uri]
@fragment = markdown_link.match(MARKDOWN_LINK_REGEX)[:fragment]
@markdown_extension = markdown_extension
@root_path = root_path
@source_markdown_directory = Pathname.new(source_markdown_filepath).dirname
Expand All @@ -34,6 +35,8 @@ def rewrite_local_links!
path = Pathname.new(uri)
@uri = "#{path.dirname + path.basename(markdown_extension).to_s.parameterize}.html"
end

@uri = "#{uri}#{fragment.empty? ? '' : '#' + fragment.parameterize}"
end

def vimwiki_markdown_file_exists?
Expand Down
16 changes: 16 additions & 0 deletions spec/lib/vimwiki_markdown/vimwiki_link_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ module VimwikiMarkdown
expect(link.uri).to eq("http://www.google.com")
end

it "should render fragment-only links correctly" do
markdown_link = "[test](#Wiki Heading)"

link = VimwikiLink.new(markdown_link, source_filepath, markdown_extension, root_path)
expect(link.title).to eq("test")
expect(link.uri).to eq("#wiki-heading")
end

context "with an existing markdown file matching name" do
let(:existing_file) { "test#{markdown_extension}" }
let(:existing_file_no_extension) { existing_file.gsub(/#{markdown_extension}$/,"") }
Expand Down Expand Up @@ -44,6 +52,14 @@ module VimwikiMarkdown
expect(link.uri).to eq("#{existing_file_no_extension}.html")
end

it "must convert same-directory markdown links with url fragments correctly" do
markdown_link = "[test](#{existing_file_no_extension}#Wiki Heading)"

link = VimwikiLink.new(markdown_link, source_filepath, markdown_extension, root_path)
expect(link.title).to eq("test")
expect(link.uri).to eq("#{existing_file_no_extension}.html#wiki-heading")
end

context "subdirectory linked files" do
let(:existing_file) { "subdirectory/test.md" }

Expand Down

0 comments on commit b67e181

Please sign in to comment.