Skip to content

Commit

Permalink
Fix white space being removed before inline tags
Browse files Browse the repository at this point in the history
  • Loading branch information
synzen committed Jan 13, 2024
1 parent 635dad4 commit e6f081d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ describe("ArticleFormatterService", () => {
});
});

// it("does not return an anchor if the href is the same as the text", async () => {
// const value =
// 'Say <a href="https://example.com">https://example.com</a> to me';

// const result = service.formatValueForDiscord(value);

// expect(result.value).toEqual("Say https://example.com to me");
// });

describe("custom placeholders", () => {
it("adds the custom placeholder if the source key exists", async () => {
const article = {
Expand Down Expand Up @@ -129,7 +138,7 @@ describe("ArticleFormatterService", () => {
],
});

expect(result.flattened["custom::test"]).toEqual(" World");
expect(result.flattened["custom::test"]).toEqual("World");
});

it("replaces matches globally", async () => {
Expand Down Expand Up @@ -344,12 +353,12 @@ describe("ArticleFormatterService", () => {
});

it("does not add new newlines", () => {
const value = `<strong>Before</strong>:`;
const value = `<p>First <strong>Before</strong>:</p>`;
service = new ArticleFormatterService();

const result = service.formatValueForDiscord(value);

expect(result.value).toEqual("**Before**:");
expect(result.value).toEqual("First **Before**:");
});
});

Expand Down Expand Up @@ -468,11 +477,23 @@ Centro comercial Moctezuma Francisco Chang Mexico

expect(result.value).toEqual(
`
https://image.com submitted by /u/FortniteStatusBot to r/FORTnITE
[link] [comments]
[ https://image.com ](https://www.reddit.com/r/FORTnITE/comments/10i5m9z/mission_alerts_1200am_utc_22jan2023/) submitted by [ /u/FortniteStatusBot ](https://www.reddit.com/user/FortniteStatusBot) to [ r/FORTnITE ](https://www.reddit.com/r/FORTnITE/)
[[link]](https://seebot.dev/images/archive/missions/22_Jan_2023.png?300) [[comments]](https://www.reddit.com/r/FORTnITE/comments/10i5m9z/mission_alerts_1200am_utc_22jan2023/)
`.trim()
);
});

describe("nested paragraphs", () => {
it("works", () => {
const val = `
<p>hello <strong>world 😀</strong> <p>another example</p></p>
`;

const result = service.formatValueForDiscord(val);

expect(result.value).toEqual("hello **world 😀** \n\nanother example");
});
});
});

describe("applySplit", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export class ArticleFormatterService {
format: "heading",
options: {
trailingLineBreaks: 0,
leadingLineBreaks: 0,
},
};

Expand Down Expand Up @@ -152,14 +153,32 @@ export class ArticleFormatterService {
format: "blockCode",
};

const pSelector: SelectorDefinition = {
selector: "p",
format: "paragraph",
};

const htmlToTextOptions: HtmlToTextOptions = {
wordwrap: false,
formatters: {
heading: (elem, walk, builder, options) => {
builder.openBlock(options);
builder.addInline("**");
builder.addLiteral("**");
walk(elem.children, builder);
builder.addInline("**");
builder.addLiteral("**");
builder.closeBlock(options);
},
paragraph: (elem, walk, builder, options) => {
builder.openBlock(options);

for (const child of elem.children) {
if (child.type === "text") {
builder.addLiteral(child.data || "");
} else {
walk([child], builder);
}
}

builder.closeBlock(options);
},
italicize: (elem, walk, builder, options) => {
Expand Down Expand Up @@ -249,6 +268,7 @@ export class ArticleFormatterService {
unorderedListSelector,
codeSelector,
preSelector,
pSelector,
],
};

Expand Down

0 comments on commit e6f081d

Please sign in to comment.