Skip to content

Commit f833807

Browse files
committed
Remove formatting for <b> and <i>
To maintain backwards compatibility with user-feeds
1 parent c8e43c0 commit f833807

3 files changed

Lines changed: 118 additions & 42 deletions

File tree

services/user-feeds-next/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
"scripts": {
2828
"build": "tsc",
2929
"start": "node ./dist/index.js",
30-
"test": "node --import tsx --test src/**/*.test.ts",
31-
"test:unit": "node --import tsx --test src/**/*.test.ts",
30+
"test": "node --import tsx --test-reporter=dot --test src/**/*.test.ts",
31+
"test:unit": "node --import tsx --test-reporter=dot --test src/**/*.test.ts",
3232
"test:e2e": "./test-e2e.sh"
3333
},
3434
"devDependencies": {

services/user-feeds-next/src/articles/formatter/article-formatter.test.ts

Lines changed: 116 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, it } from "node:test";
2-
import assert from "node:assert";
2+
import assert, { deepStrictEqual } from "node:assert";
33
import {
44
replaceTemplateString,
55
applySplit,
@@ -36,7 +36,7 @@ function createArticle(flattened: Record<string, string>): Article {
3636
};
3737
}
3838

39-
describe("article-formatter", { concurrency: true }, () => {
39+
describe("article-formatter", () => {
4040
describe("replaceTemplateString", () => {
4141
it("replaces simple placeholders", () => {
4242
const result = replaceTemplateString(
@@ -215,7 +215,9 @@ describe("article-formatter", { concurrency: true }, () => {
215215

216216
describe("Media Gallery", () => {
217217
it("filters out media gallery items with empty URLs", () => {
218-
const article = createArticle({ image1: "https://example.com/1.png" });
218+
const article = createArticle({
219+
image1: "https://example.com/1.png",
220+
});
219221
const payloads = generateDiscordPayloads(article, {
220222
componentsV2: [
221223
{
@@ -234,10 +236,15 @@ describe("article-formatter", { concurrency: true }, () => {
234236
});
235237

236238
assert.strictEqual(payloads.length, 1);
237-
const container = payloads[0]!.components![0] as { components: Array<{ items: Array<{ media: { url: string } }> }> };
239+
const container = payloads[0]!.components![0] as {
240+
components: Array<{ items: Array<{ media: { url: string } }> }>;
241+
};
238242
const gallery = container.components[0]!;
239243
assert.strictEqual(gallery.items.length, 1);
240-
assert.strictEqual(gallery.items[0]!.media.url, "https://example.com/1.png");
244+
assert.strictEqual(
245+
gallery.items[0]!.media.url,
246+
"https://example.com/1.png"
247+
);
241248
});
242249

243250
it("removes entire media gallery when all items have empty URLs", () => {
@@ -264,7 +271,9 @@ describe("article-formatter", { concurrency: true }, () => {
264271
});
265272

266273
assert.strictEqual(payloads.length, 1);
267-
const container = payloads[0]!.components![0] as { components: Array<{ type: number }> };
274+
const container = payloads[0]!.components![0] as {
275+
components: Array<{ type: number }>;
276+
};
268277
// Only the TEXT_DISPLAY should remain, gallery should be filtered out
269278
assert.strictEqual(container.components.length, 1);
270279
});
@@ -280,7 +289,10 @@ describe("article-formatter", { concurrency: true }, () => {
280289
type: "MEDIA_GALLERY",
281290
items: [
282291
{ media: { url: "https://example.com/static.png" } },
283-
{ media: { url: "https://example.com/another.png" }, description: "A description" },
292+
{
293+
media: { url: "https://example.com/another.png" },
294+
description: "A description",
295+
},
284296
],
285297
},
286298
],
@@ -289,11 +301,21 @@ describe("article-formatter", { concurrency: true }, () => {
289301
});
290302

291303
assert.strictEqual(payloads.length, 1);
292-
const container = payloads[0]!.components![0] as { components: Array<{ items: Array<{ media: { url: string }; description?: string }> }> };
304+
const container = payloads[0]!.components![0] as {
305+
components: Array<{
306+
items: Array<{ media: { url: string }; description?: string }>;
307+
}>;
308+
};
293309
const gallery = container.components[0]!;
294310
assert.strictEqual(gallery.items.length, 2);
295-
assert.strictEqual(gallery.items[0]!.media.url, "https://example.com/static.png");
296-
assert.strictEqual(gallery.items[1]!.media.url, "https://example.com/another.png");
311+
assert.strictEqual(
312+
gallery.items[0]!.media.url,
313+
"https://example.com/static.png"
314+
);
315+
assert.strictEqual(
316+
gallery.items[1]!.media.url,
317+
"https://example.com/another.png"
318+
);
297319
assert.strictEqual(gallery.items[1]!.description, "A description");
298320
});
299321

@@ -319,11 +341,21 @@ describe("article-formatter", { concurrency: true }, () => {
319341
});
320342

321343
assert.strictEqual(payloads.length, 1);
322-
const container = payloads[0]!.components![0] as { components: Array<{ items: Array<{ media: { url: string }; description?: string }> }> };
344+
const container = payloads[0]!.components![0] as {
345+
components: Array<{
346+
items: Array<{ media: { url: string }; description?: string }>;
347+
}>;
348+
};
323349
const gallery = container.components[0]!;
324350
assert.strictEqual(gallery.items.length, 1);
325-
assert.strictEqual(gallery.items[0]!.media.url, "https://example.com/dynamic.png");
326-
assert.strictEqual(gallery.items[0]!.description, "Dynamic description");
351+
assert.strictEqual(
352+
gallery.items[0]!.media.url,
353+
"https://example.com/dynamic.png"
354+
);
355+
assert.strictEqual(
356+
gallery.items[0]!.description,
357+
"Dynamic description"
358+
);
327359
});
328360

329361
it("encodes spaces in media gallery URLs", () => {
@@ -344,13 +376,20 @@ describe("article-formatter", { concurrency: true }, () => {
344376
],
345377
});
346378

347-
const container = payloads[0]!.components![0] as { components: Array<{ items: Array<{ media: { url: string } }> }> };
379+
const container = payloads[0]!.components![0] as {
380+
components: Array<{ items: Array<{ media: { url: string } }> }>;
381+
};
348382
const gallery = container.components[0]!;
349-
assert.strictEqual(gallery.items[0]!.media.url, "https://example.com/image%20with%20spaces.png");
383+
assert.strictEqual(
384+
gallery.items[0]!.media.url,
385+
"https://example.com/image%20with%20spaces.png"
386+
);
350387
});
351388

352389
it("preserves spoiler flag on media gallery items", () => {
353-
const article = createArticle({ img: "https://example.com/spoiler.png" });
390+
const article = createArticle({
391+
img: "https://example.com/spoiler.png",
392+
});
354393
const payloads = generateDiscordPayloads(article, {
355394
componentsV2: [
356395
{
@@ -365,7 +404,9 @@ describe("article-formatter", { concurrency: true }, () => {
365404
],
366405
});
367406

368-
const container = payloads[0]!.components![0] as { components: Array<{ items: Array<{ spoiler?: boolean }> }> };
407+
const container = payloads[0]!.components![0] as {
408+
components: Array<{ items: Array<{ spoiler?: boolean }> }>;
409+
};
369410
const gallery = container.components[0]!;
370411
assert.strictEqual(gallery.items[0]!.spoiler, true);
371412
});
@@ -604,7 +645,10 @@ describe("article-formatter", { concurrency: true }, () => {
604645
);
605646

606647
assert.strictEqual(enhanced[0]!.username, "Bot: John");
607-
assert.strictEqual(enhanced[0]!.avatar_url, "https://avatar.com/John.png");
648+
assert.strictEqual(
649+
enhanced[0]!.avatar_url,
650+
"https://avatar.com/John.png"
651+
);
608652
});
609653

610654
it("truncates username to 256 characters", () => {
@@ -622,6 +666,37 @@ describe("article-formatter", { concurrency: true }, () => {
622666
assert.ok(enhanced[0]!.username!.length <= 256);
623667
});
624668
});
669+
670+
describe("formatArticleForDiscord", () => {
671+
it("returns the original content formatted with markdown", async () => {
672+
const val = `<p style="text-align: left;"></p><div style="text-align: center;"><b style="font-family: arial;"><span style="font-size: large;">&nbsp;(VOSTFR)&nbsp;</span></b></div>`;
673+
674+
const result = await formatArticleForDiscord(
675+
{
676+
flattened: {
677+
id: "1",
678+
idHash: "1",
679+
title: val,
680+
},
681+
raw: {},
682+
},
683+
{
684+
customPlaceholders: [
685+
{
686+
id: "test",
687+
referenceName: "test",
688+
sourcePlaceholder: "title",
689+
steps: [],
690+
},
691+
],
692+
disableImageLinkPreviews: false,
693+
formatTables: false,
694+
stripImages: false,
695+
}
696+
);
697+
deepStrictEqual(result.customPlaceholderPreviews[0][0], "(VOSTFR)");
698+
});
699+
});
625700

626701
describe("formatValueForDiscord", () => {
627702
describe("div", () => {
@@ -668,7 +743,10 @@ describe("article-formatter", { concurrency: true }, () => {
668743
it("works with nested inline elements", () => {
669744
const value = `<a href="https://example.com"><strong>Hello World</strong></a>`;
670745
const result = formatValueForDiscord(value);
671-
assert.strictEqual(result.value, "[**Hello World**](https://example.com)");
746+
assert.strictEqual(
747+
result.value,
748+
"[**Hello World**](https://example.com)"
749+
);
672750
});
673751
});
674752

@@ -839,7 +917,10 @@ Centro comercial Moctezuma Francisco Chang Mexico
839917
<p>hello <strong>world 😀</strong> <p>another example</p></p>
840918
`;
841919
const result = formatValueForDiscord(val);
842-
assert.strictEqual(result.value, "hello **world 😀** \n\nanother example");
920+
assert.strictEqual(
921+
result.value,
922+
"hello **world 😀** \n\nanother example"
923+
);
843924
});
844925

845926
it("does not add extra newlines for empty paragraphs", () => {
@@ -930,7 +1011,14 @@ Centro comercial Moctezuma Francisco Chang Mexico
9301011
limit: 5,
9311012
isEnabled: true,
9321013
});
933-
assert.deepStrictEqual(result, ["hello", "world", ".", "hello", "world", "."]);
1014+
assert.deepStrictEqual(result, [
1015+
"hello",
1016+
"world",
1017+
".",
1018+
"hello",
1019+
"world",
1020+
".",
1021+
]);
9341022
});
9351023

9361024
it("should preserve double new lines when possible", () => {
@@ -1006,7 +1094,13 @@ Centro comercial Moctezuma Francisco Chang Mexico
10061094
isEnabled: true,
10071095
});
10081096

1009-
assert.deepStrictEqual(result, ["hello!", "world?", "fate.", "codingh", "ere!"]);
1097+
assert.deepStrictEqual(result, [
1098+
"hello!",
1099+
"world?",
1100+
"fate.",
1101+
"codingh",
1102+
"ere!",
1103+
]);
10101104
});
10111105
});
10121106

services/user-feeds-next/src/articles/formatter/article-formatter.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -149,27 +149,11 @@ export function formatValueForDiscord(
149149
},
150150
};
151151

152-
// <b> tag uses same format as <strong>
153-
const bSelector: SelectorDefinition = {
154-
selector: "b",
155-
format: "heading",
156-
options: {
157-
trailingLineBreaks: 0,
158-
leadingLineBreaks: 0,
159-
},
160-
};
161-
162152
const emSelector: SelectorDefinition = {
163153
selector: "em",
164154
format: "italicize",
165155
};
166156

167-
// <i> tag uses same format as <em>
168-
const iSelector: SelectorDefinition = {
169-
selector: "i",
170-
format: "italicize",
171-
};
172-
173157
const uSelector: SelectorDefinition = {
174158
selector: "u",
175159
format: "underline",
@@ -364,9 +348,7 @@ export function formatValueForDiscord(
364348
selectors: [
365349
imageSelector,
366350
strongSelector,
367-
bSelector,
368351
emSelector,
369-
iSelector,
370352
uSelector,
371353
anchorSelector,
372354
unorderedListSelector,

0 commit comments

Comments
 (0)