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

Create separate content parser #26

Merged
merged 1 commit into from
Aug 24, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
95 changes: 77 additions & 18 deletions lib/discourse_activity_pub/content_parser.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
# frozen_string_literal: true

# TODO (future): PR discourse/discourse to support alternate excerpts

class DiscourseActivityPub::ContentParser < ExcerptParser
class DiscourseActivityPub::ContentParser < Nokogiri::XML::SAX::Document
CUSTOM_NOTE_REGEX = /<\s*(div)[^>]*class\s*=\s*['"]note['"][^>]*>/

MARKDOWN_FEATURES = %w[
activity-pub
anchor
bbcode-block
bbcode-inline
code
Expand All @@ -25,6 +22,7 @@ class DiscourseActivityPub::ContentParser < ExcerptParser
# Compare with https://docs.joinmastodon.org/spec/activitypub/#sanitization

MARKDOWN_IT_RULES = %w[
heading
autolink
list
backticks
Expand All @@ -38,6 +36,77 @@ class DiscourseActivityPub::ContentParser < ExcerptParser
emphasis
]

attr_reader :content

def initialize(length)
@length = length
@content = +""
@current_length = 0
@start_content = false
end

def start_element(name, attributes = [])
case name
when "a"
start_tag(name, attributes)
@in_a = true
when "h1", "h2", "h3", "h4", "h5"
start_tag(name, attributes)
when "div"
if attributes.include?(%w[class note])
@content = +""
@current_length = 0
@start_content = true
end
end
end

def end_element(name)
case name
when "a"
end_tag(name)
@in_a = false
when "h1", "h2", "h3", "h4", "h5"
end_tag(name)
when "div"
throw :done if @start_content
end
end

def escape_attribute(v)
return "" unless v

v = v.dup
v.gsub!("&", "&amp;")
v.gsub!("\"", "&#34;")
v.gsub!("<", "&lt;")
v.gsub!(">", "&gt;")
v
end

def start_tag(name, attributes)
tag = name
attrs = attributes.map { |k, v| "#{k}=\"#{escape_attribute(v)}\"" }.join(" ")
tag += " #{attrs}" if attrs.present?
characters("<#{tag}>")
end

def end_tag(name)
characters("</#{name}>")
end

def characters(string)
if @current_length + string.length > @length
length = [0, @length - @current_length - 1].max
@content << string[0..length]
@content << "&hellip;"
@content << "</a>" if @in_a
throw :done
end
@content << string
@current_length += string.length
end

def self.cook(text, opts = {})
html = PrettyText.markdown(
text,
Expand Down Expand Up @@ -73,19 +142,9 @@ def self.get_note(html)
else
SiteSetting.activity_pub_note_excerpt_maxlength
end
me = self.new(length, {})
parser = Nokogiri::HTML::SAX::Parser.new(me)
catch(:done) { parser.parse(html) }
me.excerpt.strip
end

def start_element(name, attributes = [])
super

if name === "div" && attributes.include?(%w[class note])
@excerpt = +""
@current_length = 0
@start_excerpt = true
end
content_parser = self.new(length)
sax_parser = Nokogiri::HTML::SAX::Parser.new(content_parser)
catch(:done) { sax_parser.parse(html) }
content_parser.content.strip
end
end
43 changes: 43 additions & 0 deletions spec/lib/discourse_activity_pub/content_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,48 @@
expect(described_class.get_content(post)).to eq(described_class.cook(post.raw))
end
end

context "with Note" do
before do
Post.any_instance.stubs(:activity_pub_object_type).returns('Note')
end

context "with markdown" do
let(:raw_markdown) {
<<~STRING
# First Header

## Second Header

### Third Header

#### Fourth Header

Paragraph

[Link](https://discourse.org)
STRING
}
let(:cooked_markdown) {
<<~HTML
<h1>First Header</h1>
<h2>Second Header</h2>
<h3>Third Header</h3>
<h4>Fourth Header</h4>
Paragraph
<a href="https://discourse.org">Link</a>
HTML
}
let!(:post) { Fabricate(:post, raw: raw_markdown) }

before do
SiteSetting.activity_pub_note_excerpt_maxlength = 1000
end

it "returns html" do
expect(described_class.get_content(post)).to eq(cooked_markdown.strip)
end
end
end
end
end