A platform for community discussion. Free, open, simple.
UX: Preserve formatting in solution excerpts (#36552)
Régis Hanoldb1c8a0634f4
diff --git a/plugins/discourse-solved/assets/javascripts/discourse/components/solved-accepted-answer.gjs b/plugins/discourse-solved/assets/javascripts/discourse/components/solved-accepted-answer.gjsindex 3dd111ed647..aa868bdd079 100644--- a/plugins/discourse-solved/assets/javascripts/discourse/components/solved-accepted-answer.gjs+++ b/plugins/discourse-solved/assets/javascripts/discourse/components/solved-accepted-answer.gjs@@ -1,8 +1,12 @@import Component from "@glimmer/component";+import { tracked } from "@glimmer/tracking";+import { on } from "@ember/modifier";+import { action } from "@ember/object";import { service } from "@ember/service";import { htmlSafe } from "@ember/template";+import DButton from "discourse/components/d-button";import InterpolatedTranslation from "discourse/components/interpolated-translation";-import PostQuotedContent from "discourse/components/post/quoted-content";+import PostCookedHtml from "discourse/components/post/cooked-html";import UserLink from "discourse/components/user-link";import concatClass from "discourse/helpers/concat-class";import icon from "discourse/helpers/d-icon";@@ -11,6 +15,8 @@ import { i18n } from "discourse-i18n";export default class SolvedAcceptedAnswer extends Component {@service siteSettings;+ @tracked expanded = false;+get topic() {return this.args.post.topic;}@@ -27,7 +33,7 @@ export default class SolvedAcceptedAnswer extends Component {return !!this.acceptedAnswer.excerpt;}- get collapsedContent() {+ get content() {if (!this.hasExcerpt) {return "";}@@ -76,23 +82,39 @@ export default class SolvedAcceptedAnswer extends Component {return `${this.topic.url}/${postNumber}`;}+ @action+ toggleExpanded() {+ this.expanded = !this.expanded;+ }++ @action+ onClickTitle(event) {+ if (event.target.closest("a") || event.target.closest(".quote-controls")) {+ return;+ }+ this.toggleExpanded();+ }+<template>+ {{! template-lint-disable no-unnecessary-concat }}{{#if this.acceptedAnswer}}- <PostQuotedContent+ <asideclass={{concatClass- "accepted-answer"+ "quote accepted-answer"(if this.hasExcerpt "accepted-answer--has-excerpt")- (unless this.collapsedContent "title-only")+ (unless this.content "title-only")}}- @collapsedContent={{this.collapsedContent}}- @decoratorState={{@decoratorState}}- @id={{this.quoteId}}- @post={{@post}}- @quotedPostNumber={{this.acceptedAnswer.post_number}}- @quotedTopicId={{this.topic.id}}- @quotedUsername={{this.acceptedAnswer.username}}+ data-expanded="{{this.expanded}}"+ data-username={{this.acceptedAnswer.username}}+ data-post={{this.acceptedAnswer.post_number}}+ data-topic={{this.topic.id}}>- <:title>+ <div+ class="title"+ data-has-quote-controls="true"+ role={{if this.content "button"}}+ {{(if this.content (modifier on "click" this.onClickTitle))}}+ ><div class="accepted-answer--solver-accepter"><div class="accepted-answer--solver">{{#if this.showSolvedBy}}@@ -110,9 +132,7 @@ export default class SolvedAcceptedAnswer extends Component {<a href={{this.postPath}}>{{this.postNumber}}</a></Placeholder></InterpolatedTranslation>- <br />{{/if}}-</div><div class="accepted-answer--accepter">{{#if this.showMarkedBy}}@@ -129,8 +149,37 @@ export default class SolvedAcceptedAnswer extends Component {{{/if}}</div></div>- </:title>- </PostQuotedContent>+ <div class="quote-controls">+ {{#if this.content}}+ <DButton+ class="btn-flat quote-toggle"+ @action={{this.toggleExpanded}}+ @ariaControls={{this.quoteId}}+ @ariaExpanded={{this.expanded}}+ @ariaLabel={{if this.expanded "post.collapse" "expand"}}+ @title={{if this.expanded "post.collapse" "expand"}}+ @icon={{if this.expanded "chevron-up" "chevron-down"}}+ />+ {{/if}}+ <DButton+ class="btn-flat back"+ @href={{this.postPath}}+ @title="post.follow_quote"+ @ariaLabel="post.follow_quote"+ @icon="arrow-down"+ />+ </div>+ </div>+ {{#if this.content}}+ <blockquote id={{this.quoteId}}>+ <PostCookedHtml+ @post={{@post}}+ @cooked={{this.content}}+ @decoratorState={{@decoratorState}}+ />+ </blockquote>+ {{/if}}+ </aside>{{/if}}</template>}diff --git a/plugins/discourse-solved/assets/stylesheets/solutions.scss b/plugins/discourse-solved/assets/stylesheets/solutions.scssindex 945c478e9d1..81f77bcf9a1 100644--- a/plugins/discourse-solved/assets/stylesheets/solutions.scss+++ b/plugins/discourse-solved/assets/stylesheets/solutions.scss@@ -64,6 +64,10 @@ aside.quote.accepted-answer {align-items: flex-start;}+ &.accepted-answer--has-excerpt > .title {+ cursor: pointer;+ }+&:not(.accepted-answer--has-excerpt) {> .title {padding: 0.8em;@@ -114,3 +118,22 @@ aside.quote.accepted-answer {color: $solved-color;}}++aside.quote.accepted-answer[data-expanded="false"] {+ > blockquote {+ position: relative;+ max-height: 20em;+ overflow: hidden;++ &::after {+ content: "";+ position: absolute;+ bottom: 0;+ left: 0;+ right: 0;+ height: 3em;+ background: linear-gradient(transparent, var(--secondary));+ pointer-events: none;+ }+ }+}diff --git a/plugins/discourse-solved/lib/discourse_solved/topic_extension.rb b/plugins/discourse-solved/lib/discourse_solved/topic_extension.rbindex c85b5eed6e0..c3494dda11e 100644--- a/plugins/discourse-solved/lib/discourse_solved/topic_extension.rb+++ b/plugins/discourse-solved/lib/discourse_solved/topic_extension.rb@@ -12,16 +12,7 @@ module DiscourseSolved::TopicExtensionanswer_post_user = answer_post.user || Discourse.system_useraccepter = solved.accepter || self.user || Discourse.system_user- excerpt =- if SiteSetting.solved_quote_length > 0- PrettyText.excerpt(- answer_post.cooked,- SiteSetting.solved_quote_length,- keep_emoji_images: true,- )- else- nil- end+ excerpt = SiteSetting.solved_quote_length > 0 ? answer_post.cooked : nilaccepted_answer = {post_number: answer_post.post_number,diff --git a/plugins/discourse-solved/spec/integration/solved_spec.rb b/plugins/discourse-solved/spec/integration/solved_spec.rbindex 32beb45e2f9..cde46c129bc 100644--- a/plugins/discourse-solved/spec/integration/solved_spec.rb+++ b/plugins/discourse-solved/spec/integration/solved_spec.rb@@ -649,7 +649,7 @@ RSpec.describe "Managing Posts solved status" doexpect(accepted_message.data[:accepted_answer][:post_number]).to eq(2)expect(accepted_message.data[:accepted_answer][:username]).to eq(user.username)expect(accepted_message.data[:accepted_answer][:name]).to eq(user.name)- expect(accepted_message.data[:accepted_answer][:excerpt]).to eq(reply.excerpt)+ expect(accepted_message.data[:accepted_answer][:excerpt]).to eq(reply.cooked)expect(accepted_message.data[:accepted_answer][:accepter_name]).to eq(admin.name)expect(accepted_message.data[:accepted_answer][:accepter_username]).to eq(admin.username)diff --git a/plugins/discourse-solved/spec/system/solved_spec.rb b/plugins/discourse-solved/spec/system/solved_spec.rbindex a7be0d739a5..2bc69edb665 100644--- a/plugins/discourse-solved/spec/system/solved_spec.rb+++ b/plugins/discourse-solved/spec/system/solved_spec.rb@@ -57,6 +57,45 @@ describe "Solved", type: :system doexpect(page.find(".post-list")).to have_content(solver_post.cooked)end+ describe "solution excerpt formatting" do+ it "preserves code blocks in the solution excerpt" do+ raw = <<~RAW+ Here's the solution:++ ```ruby+ def hello+ puts "world"+ end+ ```++ Hope this helps!+ RAW+ code_solution_post = Fabricate(:post, topic:, user: admin, raw:)+ Fabricate(:solved_topic, topic:, answer_post: code_solution_post, accepter:)++ sign_in(accepter)+ topic_page.visit_topic(topic)++ within("#{ACCEPTED_ANSWER_QUOTE_SELECTOR} blockquote") do+ expect(page).to have_css("pre code.lang-ruby")+ expect(page).to have_content("def hello")+ expect(page).to have_content('puts "world"')+ end+ end++ it "preserves images in the solution excerpt" do+ upload = Fabricate(:upload)+ raw = "Check this image: "+ image_solution_post = Fabricate(:post, topic:, user: admin, raw:)+ Fabricate(:solved_topic, topic:, answer_post: image_solution_post, accepter:)++ sign_in(accepter)+ topic_page.visit_topic(topic)++ within("#{ACCEPTED_ANSWER_QUOTE_SELECTOR} blockquote") { expect(page).to have_css("img") }+ end+ end+privatedef visit_solver_postdiff --git a/plugins/discourse-solved/test/javascripts/acceptance/discourse-solved-test.js b/plugins/discourse-solved/test/javascripts/acceptance/discourse-solved-test.jsindex f0b359f7d8e..c409d70a059 100644--- a/plugins/discourse-solved/test/javascripts/acceptance/discourse-solved-test.js+++ b/plugins/discourse-solved/test/javascripts/acceptance/discourse-solved-test.js@@ -21,12 +21,16 @@ acceptance(`Discourse Solved Plugin`, function (needs) {);await visit("/t/with-excerpt/11");- assert.dom(".quote blockquote").hasText("this is an excerpt");++ assert.dom(".quote.accepted-answer blockquote").exists();+ assert+ .dom(".quote.accepted-answer blockquote")+ .hasText("this is an excerpt");await visit("/t/without-excerpt/12");- assert.dom(".quote blockquote").hasNoText();- assert.dom(".quote.title-only .title").exists();+ assert.dom(".quote.accepted-answer.title-only").exists();+ assert.dom(".quote.accepted-answer blockquote").doesNotExist();});test("Full page search displays solved status", async function (assert) {diff --git a/plugins/discourse-solved/test/javascripts/helpers/discourse-solved-helpers.js b/plugins/discourse-solved/test/javascripts/helpers/discourse-solved-helpers.jsindex ce733d528e6..f1bce61c085 100644--- a/plugins/discourse-solved/test/javascripts/helpers/discourse-solved-helpers.js+++ b/plugins/discourse-solved/test/javascripts/helpers/discourse-solved-helpers.js@@ -1,209 +1,207 @@-export const postStreamWithAcceptedAnswerExcerpt = (excerpt) => {- return {- post_stream: {- posts: [- {- id: 21,- name: null,- username: "kzh",- avatar_template: "/letter_avatar_proxy/v2/letter/k/ac91a4/{size}.png",- created_at: "2017-08-08T20:11:32.542Z",- cooked: "<p>How do I declare a variable in ruby?</p>",- post_number: 1,- post_type: 1,- updated_at: "2017-08-08T21:03:30.521Z",- reply_count: 0,- reply_to_post_number: null,- quote_count: 0,- avg_time: null,- incoming_link_count: 0,- reads: 1,- score: 0,- yours: true,- topic_id: 23,- topic_slug: "test-solved",- display_username: null,- primary_group_name: null,- primary_group_flair_url: null,- primary_group_flair_bg_color: null,- primary_group_flair_color: null,- version: 2,- can_edit: true,- can_delete: false,- can_recover: null,- can_wiki: true,- read: true,- user_title: null,- actions_summary: [- { id: 3, can_act: true },- { id: 4, can_act: true },- { id: 5, hidden: true, can_act: true },- { id: 7, can_act: true },- { id: 8, can_act: true },- ],- moderator: false,- admin: true,- staff: true,- user_id: 1,- hidden: false,- hidden_reason_id: null,- trust_level: 4,- deleted_at: null,- user_deleted: false,- edit_reason: null,- can_view_edit_history: true,- wiki: false,- can_accept_answer: false,- can_unaccept_answer: false,- accepted_answer: false,- },- {- id: 22,- name: null,- username: "kzh",- avatar_template: "/letter_avatar_proxy/v2/letter/k/ac91a4/{size}.png",- created_at: "2017-08-08T20:12:04.657Z",- cooked:- "<p>this is a long answer that potentially solves the question</p>",- post_number: 2,- post_type: 1,- updated_at: "2017-08-08T21:20:24.417Z",- reply_count: 0,- reply_to_post_number: null,- quote_count: 0,- avg_time: null,- incoming_link_count: 0,- reads: 1,- score: 0,- yours: true,- topic_id: 23,- topic_slug: "test-solved",- display_username: null,- primary_group_name: null,- primary_group_flair_url: null,- primary_group_flair_bg_color: null,- primary_group_flair_color: null,- version: 2,- can_edit: true,- can_delete: true,- can_recover: null,- can_wiki: true,- read: true,- user_title: null,- actions_summary: [- { id: 3, can_act: true },- { id: 4, can_act: true },- { id: 5, hidden: true, can_act: true },- { id: 7, can_act: true },- { id: 8, can_act: true },- ],- moderator: false,- admin: true,- staff: true,- user_id: 1,- hidden: false,- hidden_reason_id: null,- trust_level: 4,- deleted_at: null,- user_deleted: false,- edit_reason: null,- can_view_edit_history: true,- wiki: false,- can_accept_answer: true,- accepted_answer: true,- },- ],- stream: [21, 22],- },- timeline_lookup: [[1, 0]],- id: 23,- title: "Test solved",- fancy_title: "Test solved",- posts_count: 2,- created_at: "2017-08-08T20:11:32.098Z",- views: 6,- reply_count: 0,- participant_count: 1,- like_count: 0,- last_posted_at: "2017-08-08T20:12:04.657Z",- visible: true,- closed: false,- archived: false,- has_summary: false,- archetype: "regular",- slug: "test-solved",- category_id: 1,- word_count: 18,- deleted_at: null,- pending_posts_count: 0,- user_id: 1,- pm_with_non_human_user: false,- draft: null,- draft_key: "topic_23",- draft_sequence: 6,- posted: true,- unpinned: null,- pinned_globally: false,- pinned: false,- pinned_at: null,- pinned_until: null,- details: {- created_by: {- id: 1,+export const postStreamWithAcceptedAnswerExcerpt = (excerpt) => ({+ post_stream: {+ posts: [+ {+ id: 21,+ name: null,username: "kzh",avatar_template: "/letter_avatar_proxy/v2/letter/k/ac91a4/{size}.png",+ created_at: "2017-08-08T20:11:32.542Z",+ cooked: "<p>How do I declare a variable in ruby?</p>",+ post_number: 1,+ post_type: 1,+ updated_at: "2017-08-08T21:03:30.521Z",+ reply_count: 0,+ reply_to_post_number: null,+ quote_count: 0,+ avg_time: null,+ incoming_link_count: 0,+ reads: 1,+ score: 0,+ yours: true,+ topic_id: 23,+ topic_slug: "test-solved",+ display_username: null,+ primary_group_name: null,+ primary_group_flair_url: null,+ primary_group_flair_bg_color: null,+ primary_group_flair_color: null,+ version: 2,+ can_edit: true,+ can_delete: false,+ can_recover: null,+ can_wiki: true,+ read: true,+ user_title: null,+ actions_summary: [+ { id: 3, can_act: true },+ { id: 4, can_act: true },+ { id: 5, hidden: true, can_act: true },+ { id: 7, can_act: true },+ { id: 8, can_act: true },+ ],+ moderator: false,+ admin: true,+ staff: true,+ user_id: 1,+ hidden: false,+ hidden_reason_id: null,+ trust_level: 4,+ deleted_at: null,+ user_deleted: false,+ edit_reason: null,+ can_view_edit_history: true,+ wiki: false,+ can_accept_answer: false,+ can_unaccept_answer: false,+ accepted_answer: false,},- last_poster: {- id: 1,+ {+ id: 22,+ name: null,username: "kzh",avatar_template: "/letter_avatar_proxy/v2/letter/k/ac91a4/{size}.png",+ created_at: "2017-08-08T20:12:04.657Z",+ cooked:+ "<p>this is a long answer that potentially solves the question</p>",+ post_number: 2,+ post_type: 1,+ updated_at: "2017-08-08T21:20:24.417Z",+ reply_count: 0,+ reply_to_post_number: null,+ quote_count: 0,+ avg_time: null,+ incoming_link_count: 0,+ reads: 1,+ score: 0,+ yours: true,+ topic_id: 23,+ topic_slug: "test-solved",+ display_username: null,+ primary_group_name: null,+ primary_group_flair_url: null,+ primary_group_flair_bg_color: null,+ primary_group_flair_color: null,+ version: 2,+ can_edit: true,+ can_delete: true,+ can_recover: null,+ can_wiki: true,+ read: true,+ user_title: null,+ actions_summary: [+ { id: 3, can_act: true },+ { id: 4, can_act: true },+ { id: 5, hidden: true, can_act: true },+ { id: 7, can_act: true },+ { id: 8, can_act: true },+ ],+ moderator: false,+ admin: true,+ staff: true,+ user_id: 1,+ hidden: false,+ hidden_reason_id: null,+ trust_level: 4,+ deleted_at: null,+ user_deleted: false,+ edit_reason: null,+ can_view_edit_history: true,+ wiki: false,+ can_accept_answer: true,+ accepted_answer: true,},- participants: [- {- id: 1,- username: "kzh",- avatar_template: "/letter_avatar_proxy/v2/letter/k/ac91a4/{size}.png",- post_count: 2,- primary_group_name: null,- primary_group_flair_url: null,- primary_group_flair_color: null,- primary_group_flair_bg_color: null,- },- ],- notification_level: 3,- notifications_reason_id: 1,- can_move_posts: true,- can_edit: true,- can_delete: true,- can_remove_allowed_users: true,- can_invite_to: true,- can_invite_via_email: true,- can_create_post: true,- can_reply_as_new_topic: true,- can_flag_topic: true,- },- highest_post_number: 2,- last_read_post_number: 2,- last_read_post_id: 22,- deleted_by: null,- has_deleted: false,- actions_summary: [- { id: 4, count: 0, hidden: false, can_act: true },- { id: 7, count: 0, hidden: false, can_act: true },- { id: 8, count: 0, hidden: false, can_act: true },],- chunk_size: 20,- bookmarked: false,- tags: [],- featured_link: null,- topic_timer: null,- message_bus_last_id: 0,- accepted_answer: {- post_number: 2,+ stream: [21, 22],+ },+ timeline_lookup: [[1, 0]],+ id: 23,+ title: "Test solved",+ fancy_title: "Test solved",+ posts_count: 2,+ created_at: "2017-08-08T20:11:32.098Z",+ views: 6,+ reply_count: 0,+ participant_count: 1,+ like_count: 0,+ last_posted_at: "2017-08-08T20:12:04.657Z",+ visible: true,+ closed: false,+ archived: false,+ has_summary: false,+ archetype: "regular",+ slug: "test-solved",+ category_id: 1,+ word_count: 18,+ deleted_at: null,+ pending_posts_count: 0,+ user_id: 1,+ pm_with_non_human_user: false,+ draft: null,+ draft_key: "topic_23",+ draft_sequence: 6,+ posted: true,+ unpinned: null,+ pinned_globally: false,+ pinned: false,+ pinned_at: null,+ pinned_until: null,+ details: {+ created_by: {+ id: 1,+ username: "kzh",+ avatar_template: "/letter_avatar_proxy/v2/letter/k/ac91a4/{size}.png",+ },+ last_poster: {+ id: 1,username: "kzh",- excerpt,- accepter_username: "tomtom",- accepter_name: "Tomtom",+ avatar_template: "/letter_avatar_proxy/v2/letter/k/ac91a4/{size}.png",},- };-};+ participants: [+ {+ id: 1,+ username: "kzh",+ avatar_template: "/letter_avatar_proxy/v2/letter/k/ac91a4/{size}.png",+ post_count: 2,+ primary_group_name: null,+ primary_group_flair_url: null,+ primary_group_flair_color: null,+ primary_group_flair_bg_color: null,+ },+ ],+ notification_level: 3,+ notifications_reason_id: 1,+ can_move_posts: true,+ can_edit: true,+ can_delete: true,+ can_remove_allowed_users: true,+ can_invite_to: true,+ can_invite_via_email: true,+ can_create_post: true,+ can_reply_as_new_topic: true,+ can_flag_topic: true,+ },+ highest_post_number: 2,+ last_read_post_number: 2,+ last_read_post_id: 22,+ deleted_by: null,+ has_deleted: false,+ actions_summary: [+ { id: 4, count: 0, hidden: false, can_act: true },+ { id: 7, count: 0, hidden: false, can_act: true },+ { id: 8, count: 0, hidden: false, can_act: true },+ ],+ chunk_size: 20,+ bookmarked: false,+ tags: [],+ featured_link: null,+ topic_timer: null,+ message_bus_last_id: 0,+ accepted_answer: {+ post_number: 2,+ username: "kzh",+ excerpt: excerpt ? `<p>${excerpt}</p>` : excerpt,+ accepter_username: "tomtom",+ accepter_name: "Tomtom",+ },+});