mirror of
https://github.com/System-End/highway.git
synced 2026-04-19 16:28:24 +00:00
Attempt to fix project rendering for images
This commit is contained in:
parent
7e257d5db3
commit
91725b1c47
3 changed files with 39 additions and 7 deletions
|
|
@ -2,11 +2,42 @@ module MarkdownRenderable
|
|||
extend ActiveSupport::Concern
|
||||
|
||||
class_methods do
|
||||
def render_markdown(text)
|
||||
def render_markdown(text, user = nil, project_name = nil)
|
||||
return "" if text.blank?
|
||||
|
||||
@markdown_renderer ||= Redcarpet::Markdown.new(
|
||||
Redcarpet::Render::HTML,
|
||||
renderer = if user && project_name
|
||||
Class.new(Redcarpet::Render::HTML) do
|
||||
def initialize(user, project_name)
|
||||
@user = user
|
||||
@project_name = project_name
|
||||
super()
|
||||
end
|
||||
|
||||
def image(link, title, alt_text)
|
||||
# If the link is a relative path, rewrite it
|
||||
unless link =~ %r{^https?://}
|
||||
link = "/projects/#{@user}/#{@project_name}/#{link}"
|
||||
end
|
||||
"<img src=\"#{link}\" alt=\"#{alt_text}\" title=\"#{title}\">"
|
||||
end
|
||||
|
||||
def postprocess(full_document)
|
||||
# Only rewrite src attributes in img tags that don't already have the project path
|
||||
full_document.gsub(/<img[^>]+src="([^"]+)"[^>]*>/) do |match|
|
||||
src = $1
|
||||
unless src =~ %r{^https?://} || src.start_with?("/projects/#{@user}/#{@project_name}/")
|
||||
src = "/projects/#{@user}/#{@project_name}/#{src}"
|
||||
end
|
||||
match.gsub(/src="[^"]+"/, "src=\"#{src}\"")
|
||||
end
|
||||
end
|
||||
end.new(user, project_name)
|
||||
else
|
||||
Redcarpet::Render::HTML
|
||||
end
|
||||
|
||||
markdown = Redcarpet::Markdown.new(
|
||||
renderer,
|
||||
autolink: true,
|
||||
tables: true,
|
||||
fenced_code_blocks: true,
|
||||
|
|
@ -18,13 +49,13 @@ module MarkdownRenderable
|
|||
footnotes: true
|
||||
)
|
||||
|
||||
@markdown_renderer.render(text).html_safe
|
||||
markdown.render(text).html_safe
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def render_markdown(text)
|
||||
self.class.render_markdown(text)
|
||||
def render_markdown(text, user = nil, project_name = nil)
|
||||
self.class.render_markdown(text, user, project_name)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -135,7 +135,7 @@ class Project
|
|||
author: metadata["author"],
|
||||
description: metadata["description"],
|
||||
created_at: metadata["created_at"],
|
||||
content: render_markdown(markdown_content)
|
||||
content: render_markdown(markdown_content, user, project_name)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
1
public/projects
Symbolic link
1
public/projects
Symbolic link
|
|
@ -0,0 +1 @@
|
|||
../content/projects
|
||||
Loading…
Add table
Reference in a new issue