Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add processing for url fragments to vimwiki_link #23

Merged
merged 1 commit into from
Feb 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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