Skip to content

Commit

Permalink
Merge pull request #43 from DannyBen/patch
Browse files Browse the repository at this point in the history
Stop tampering with links
  • Loading branch information
DannyBen authored May 12, 2018
2 parents 0bbfe50 + 07952a4 commit fd4e578
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 88 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Feature Highlights

- Easy to use
- Built in full text search
- Built in GraphViz diagram generator
- Compatible with how markdown files are displayed on GitHub andGitHub pages.
- Configure with a configuration file or command arguments


Expand Down Expand Up @@ -129,10 +129,7 @@ You can put images and any other asset file anywhere in your documentation
folder.

When linking to other pages or images in your documentation folder, simply
use the URL relative to the markdown file. Madness will convert these to be
relative to the docroot of the generated site.

This behavior mimics how GitHub is rendering markdown files.
use the URL relative to the markdown file.

For example, if you have a folder named `subfolder` that contains a
`README.md` and a `nice-picture.png`, showing it in your `README` is done by
Expand Down Expand Up @@ -161,15 +158,18 @@ will be automatically added based on the file name.
Hidden Directories
--------------------------------------------------

Directories that begin with an underscore will not be displayed in the
navigation.
These directories will not be displayed in the navigation:

- Directories that begin with an underscore.
- Directories that are made only of lowercase letters, underscoew, dash and/or
numbers (`/^[a-z_\-0-9]+$/`).



Docker Image
--------------------------------------------------

This gem is also available as a docker image.
Madness server is also available as a docker image.

This command will start the server on localhost:3000, with the current
directory as the markdown documentation folder
Expand Down
20 changes: 2 additions & 18 deletions lib/madness/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def initialize(path)
@path = path

base = path.empty? ? docroot : "#{docroot}/#{path}"
base.chomp! '/'

if File.directory? base
@file = "#{base}/README.md"
Expand Down Expand Up @@ -48,26 +49,21 @@ def content!

# Return a reasonable HTML title for the file or directory
def title
if file =~ /README.md/
if type == :readme
result = File.basename File.dirname(file)
else
result = File.basename(file,'.md')
end
result.tr '-', ' '
end

def relative_dir
@relative_dir ||= dir[/#{docroot}\/(.*)/,1]
end

private

# Convert markdown to HTML, wit hsome additional processing:
# 1. Syntax highilghting
# 2. Prepend H1 if needed
def markdown_to_html
doc = CommonMarker.render_doc(File.read file)
fix_relative_links doc if relative_dir
html = doc.to_html
html = syntax_highlight(html) if config.highlighter
html = prepend_h1(html) if config.autoh1
Expand Down Expand Up @@ -99,18 +95,6 @@ def syntax_highlight(html)
CodeRay.scan(code, lang).html opts
end
end

def fix_relative_links(doc)
doc.walk do |node|
if [:link, :image].include? node.type
node.url = "/#{relative_dir}/#{node.url}" if relative? node.url
end
end
end

def relative?(link)
!(link.include? ':' or link[0] == '/')
end
end
end

2 changes: 1 addition & 1 deletion lib/madness/navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def get_dirs(dir)
dirs = Dir["#{dir}/*"].select { |f| File.directory? f }
dirs.reject! do |f|
basename = File.basename(f)
basename[0] == '_'
basename[0] == '_' or basename =~ /^[a-z_\-0-9]+$/
end
dirs
end
Expand Down
6 changes: 5 additions & 1 deletion lib/madness/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ class Server < ServerBase
get '/*' do
path = params[:splat].first

doc = Document.new path
doc = Document.new path
dir = doc.dir
content = doc.content

if doc.type == :readme and !path.empty? and path[-1] != '/'
redirect "#{path}/"
end

nav = Navigation.new(dir)
breadcrumbs = Breadcrumbs.new(path).links

Expand Down
7 changes: 0 additions & 7 deletions spec/fixtures/docroot/Links Folder/Page.md

This file was deleted.

7 changes: 0 additions & 7 deletions spec/fixtures/docroot/Links Folder/README.md

This file was deleted.

7 changes: 0 additions & 7 deletions spec/fixtures/docroot/Links.md

This file was deleted.

36 changes: 0 additions & 36 deletions spec/madness/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,48 +70,12 @@
expect(doc.content).not_to include ' &amp; '
end

it "does not alter URLs that begin with a slash" do
doc = Document.new "Links Folder/Page"
expect(doc.content).to have_tag 'a', with: { href: '/abso.loot' }
end

it "does not alter URLs that contain a colon" do
doc = Document.new "Links Folder/Page"
expect(doc.content).to have_tag 'a', with: { href: 'tel:55512345' }
expect(doc.content).to have_tag 'a', with: { href: 'http://example.com' }
end

context "with auto h1 disabled" do
it "does not add h1" do
config.autoh1 = false
doc = Document.new "File without H1"
expect(doc.content).not_to have_tag :h1
end
end

context "with a file in the root path" do
it "does not alter relative URLs" do
doc = Document.new "Links"
expect(doc.content).to have_tag 'a', with: { href: 'somewhere.html' }
expect(doc.content).to have_tag 'img', with: { src: 'ok.png' }
end
end

context "with a README file in a subfolder" do
it "modifies relative URLs to be relative to docroot" do
doc = Document.new "Links Folder"
expect(doc.content).to have_tag 'a', with: { href: '/Links%20Folder/somewhere.html' }
expect(doc.content).to have_tag 'img', with: { src: '/Links%20Folder/ok.png' }
end
end

context "with a regular file in a subfolder" do
it "modifies relative URLs to be relative to docroot" do
doc = Document.new "Links Folder/Page"
expect(doc.content).to have_tag 'a', with: { href: '/Links%20Folder/somewhere.html' }
expect(doc.content).to have_tag 'img', with: { src: '/Links%20Folder/ok.png' }
end
end

end
end
12 changes: 9 additions & 3 deletions spec/madness/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,19 @@

context "in subfolders" do
it "works" do
get '/Folder'
get '/Folder/'
expect(last_response).to be_ok
expect(last_response.body).to have_tag 'h1', text: "Sub folder #1"
end

it "shows breadcrumbs" do
it "redirects to have a trailing slash" do
get '/Folder'
expect(last_response).to be_redirection
expect(last_response.location).to eq 'http://example.org/Folder/'
end

it "shows breadcrumbs" do
get '/Folder/'
expect(last_response.body).to have_tag '.breadcrumbs' do
with_tag 'a', text: 'Home'
with_tag 'span', text: 'Folder'
Expand Down Expand Up @@ -106,7 +112,7 @@
end

it "does not redirect if the file is README" do
get '/No%20Redirect'
get '/No%20Redirect/'
expect(last_response).to be_ok
expect(last_response.body).to have_tag 'p', text: "Was not redirected"
end
Expand Down

0 comments on commit fd4e578

Please sign in to comment.