FEATURE: Export topics to markdown (#15615)

* FEATURE: Export topics to markdown

The route `/raw/TOPIC_ID` will now export whole topics (paginated to 100
posts) in a markdown format.

See https://meta.discourse.org/t/-/152185/12
This commit is contained in:
Rafael dos Santos Silva 2022-01-17 18:05:14 -03:00 committed by GitHub
parent 2909b8b820
commit 3f91c8835b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 1 deletions

View File

@ -28,6 +28,8 @@ class PostsController < ApplicationController
:user_posts_feed
]
MARKDOWN_TOPIC_PAGE_SIZE ||= 100
def markdown_id
markdown Post.find(params[:id].to_i)
end
@ -36,8 +38,23 @@ class PostsController < ApplicationController
if params[:revision].present?
post_revision = find_post_revision_from_topic_id
render plain: post_revision.modifications[:raw].last
elsif params[:post_number].present?
markdown Post.find_by(topic_id: params[:topic_id].to_i, post_number: params[:post_number].to_i)
else
markdown Post.find_by(topic_id: params[:topic_id].to_i, post_number: (params[:post_number] || 1).to_i)
opts = params.slice(:page)
opts[:limit] = MARKDOWN_TOPIC_PAGE_SIZE
topic_view = TopicView.new(params[:topic_id], current_user, opts)
content = topic_view.posts.map do |p|
<<~HEREDOC
#{p.user.username} | #{p.updated_at} | ##{p.post_number}
#{p.raw}
-------------------------
HEREDOC
end
render plain: content.join
end
end

View File

@ -1888,6 +1888,16 @@ describe PostsController do
expect(response.status).to eq(200)
expect(response.body).to eq("123456789")
end
it "can show whole topics" do
topic = Fabricate(:topic)
post = Fabricate(:post, topic: topic, post_number: 1, raw: "123456789")
post_2 = Fabricate(:post, topic: topic, post_number: 2, raw: "abcdefghij")
post.save
get "/raw/#{topic.id}"
expect(response.status).to eq(200)
expect(response.body).to include("123456789", "abcdefghij")
end
end
describe '#short_link' do