mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-23 23:29:39 +02:00
* Add help articles * Broadcast sync messages as notifications * Lint fixes * more lint fixes * Remove redundant code
54 lines
1.1 KiB
Ruby
54 lines
1.1 KiB
Ruby
class Help::Article
|
|
attr_reader :frontmatter, :content
|
|
|
|
def initialize(frontmatter:, content:)
|
|
@frontmatter = frontmatter
|
|
@content = content
|
|
end
|
|
|
|
def title
|
|
frontmatter["title"]
|
|
end
|
|
|
|
def html
|
|
render_markdown(content)
|
|
end
|
|
|
|
class << self
|
|
def root_path
|
|
Rails.root.join("docs", "help")
|
|
end
|
|
|
|
def find(slug)
|
|
Dir.glob(File.join(root_path, "*.md")).each do |file_path|
|
|
file_content = File.read(file_path)
|
|
frontmatter, markdown_content = parse_frontmatter(file_content)
|
|
|
|
return new(frontmatter:, content: markdown_content) if frontmatter["slug"] == slug
|
|
end
|
|
|
|
nil
|
|
end
|
|
|
|
private
|
|
|
|
def parse_frontmatter(content)
|
|
if content =~ /\A---(.+?)---/m
|
|
frontmatter = YAML.safe_load($1)
|
|
markdown_content = content[($~.end(0))..-1].strip
|
|
else
|
|
frontmatter = {}
|
|
markdown_content = content
|
|
end
|
|
|
|
[ frontmatter, markdown_content ]
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def render_markdown(content)
|
|
markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML)
|
|
markdown.render(content)
|
|
end
|
|
end
|