FIX: Allow new hashtag HTML to be quoted to markdown (#19117)

Follow up from d3f02a1270

This commit fixes post quoting so that if the new
hashtag-cooked HTML is selected, we convert back to
a regular plain text #hashtag with the correct type and ref.
This commit is contained in:
Martin Brennan 2022-11-21 12:04:46 +10:00 committed by GitHub
parent 86ae75f8d5
commit 3846b6248f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 0 deletions

View File

@ -315,6 +315,18 @@ export class Tag {
return text;
}
if ("hashtag-cooked" === attr.class) {
if (attr["data-ref"]) {
return `#${attr["data-ref"]}`;
} else {
let type = "";
if (attr["data-type"]) {
type = `::${attr["data-type"]}`;
}
return `#${attr["data-slug"]}${type}`;
}
}
let img;
if (
["lightbox", "d-lazyload"].includes(attr.class) &&

View File

@ -353,6 +353,30 @@ helloWorld();</code>consectetur.`;
assert.strictEqual(toMarkdown(html), markdown);
});
test("keeps hashtag-cooked and converts to bare hashtag with type", function (assert) {
const html = `
<p dir="ltr">This is <a class="hashtag-cooked" href="/c/ux/14" data-type="category" data-slug="ux">
<svg class="fa d-icon d-icon-folder svg-icon svg-node">
<use href="#folder"></use>
</svg>
<span>ux</span>
</a> and <a class="hashtag-cooked" href="/tag/design" data-slug="design">
<svg class="fa d-icon d-icon-tag svg-icon svg-node">
<use href="#tag"></use>
</svg>
<span>design</span>
</a> and <a class="hashtag-cooked" href="/c/uncategorized/design/22" data-type="category" data-slug="design" data-ref="uncategorized:design">
<svg class="fa d-icon d-icon-folder svg-icon svg-node">
<use href="#folder"></use>
</svg>
<span>design</span>
</a></p>
`;
const markdown = `This is #ux::category and #design and #uncategorized:design`;
assert.strictEqual(toMarkdown(html), markdown);
});
test("keeps emoji and removes click count", function (assert) {
const html = `
<p>

View File

@ -25,12 +25,22 @@ function addHashtag(buffer, matches, state) {
let token;
if (result) {
token = new state.Token("link_open", "a", 1);
// Data attributes here are used later on for things like quoting
// HTML-to-markdown
token.attrs = [
["class", "hashtag-cooked"],
["href", result.relative_url],
["data-type", result.type],
["data-slug", result.slug],
];
// Most cases these will be the exact same, one standout is categories
// which have a parent:child reference.
if (result.slug !== result.ref) {
token.attrs.push(["data-ref", result.ref]);
}
token.block = false;
buffer.push(token);
@ -127,6 +137,7 @@ export function setup(helper) {
"span.hashtag-raw",
"a[data-type]",
"a[data-slug]",
"a[data-ref]",
])
);
}