Skip to content

Commit

Permalink
Do not replace <a> links with their href
Browse files Browse the repository at this point in the history
Instead replacing it with the <a> tag's content. When duplicating google calendar events, the links turn into a google redirect which is annoying, so instead we can just use the text content of the <a> tag which remains the same
  • Loading branch information
QuantumManiac committed Jan 13, 2024
1 parent e7a9509 commit 52ec833
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 41 deletions.
17 changes: 1 addition & 16 deletions src/utils/calendarDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,32 +46,17 @@ export function splitDescription(description: string): {
};
}

/**
* Replace <a> tags with their href attribute to allow for cleaner parsing of the description
* @param html The html to replace the <a> tags in
* @returns The html with the <a> tags replaced with their href attribute
*/
export function replaceATagsWithHref(html: string): string {
// Replace <a> tags with their href attribute
// TODO replace this with a custom html-to-text formatter:
// https://github.com/html-to-text/node-html-to-text/blob/master/packages/html-to-text/README.md#override-formatting
const aTagRegex = /<a href="(.*)">(.*)<\/a>/g;
return html.replace(aTagRegex, "$1");
}

/**
* Parse the description from HTML to plain text
* @param description The description to parse
* @returns The parsed description
*/
export function parseDescriptionFromHtml(description: string): string {
// Replace <a> tags with their href attribute
description = replaceATagsWithHref(description);

// Convert HTML to plain text
// TODO parse description as markdown potentially - would allow for slack to have more formatting
const plainDescription = convert(description, {
wordwrap: false,
selectors: [{ selector: "a", options: { ignoreHref: true } }],
});

return plainDescription;
Expand Down
42 changes: 17 additions & 25 deletions tests/unit/utils/calendarDescription.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
splitDescription,
replaceATagsWithHref,
parseDescriptionFromHtml,
parseDescription,
} from "../../../src/utils/calendarDescription";
import { splitDescription, parseDescriptionFromHtml, parseDescription } from "../../../src/utils/calendarDescription";

import { slackChannels } from "../../fixtures/slackChannels";

Expand Down Expand Up @@ -101,30 +96,27 @@ describe("utils/calendarDescription", () => {
});
});

describe("replaceATagsWithHref", () => {
it("should replace <a> tags with their href", () => {
const html = `<a href="https://example.com">Example</a>`;
const result = replaceATagsWithHref(html);
expect(result).toEqual("https://example.com");
describe("parseDescriptionFromHtml", () => {
it("should properly parse the HTML description", () => {
const description = `This is a description<br>With a link: <a href="https://example.com">https://example.com</a><br>And another line<br>---<br>foo: bar`;
const result = parseDescriptionFromHtml(description);
expect(result).toEqual(
"This is a description\nWith a link: https://example.com\nAnd another line\n---\nfoo: bar",
);
});

it("should ignore <a> tags that don't have an href", () => {
const html = `<a>Example</a>`;
const result = replaceATagsWithHref(html);
expect(result).toEqual(html);
it("should parse <a> tags with href different than the content", () => {
const description = `This is a description<br>With a link: <a href="https://example2.com">https://example.com</a><br>And another line<br>---<br>foo: bar`;
const result = parseDescriptionFromHtml(description);
expect(result).toEqual(
"This is a description\nWith a link: https://example.com\nAnd another line\n---\nfoo: bar",
);
});

it("should work even if the <a> tag does not contain anything inside it", () => {
const html = `<a href="https://example.com"></a>`;
const result = replaceATagsWithHref(html);
expect(result).toEqual("https://example.com");
});
});
describe("parseDescriptionFromHtml", () => {
it("should properly parse the HTML description", () => {
const description = `This is a description<br>With a <a href="https://example.com">link</a><br>And another line<br>---<br>foo: bar`;
it("should parse <a> tags with href but no content", () => {
const description = `This is a description<br>With no link:<a href="https://example2.com"></a><br>And another line<br>---<br>foo: bar`;
const result = parseDescriptionFromHtml(description);
expect(result).toEqual("This is a description\nWith a https://example.com\nAnd another line\n---\nfoo: bar");
expect(result).toEqual("This is a description\nWith no link:\nAnd another line\n---\nfoo: bar");
});
});

Expand Down

0 comments on commit 52ec833

Please sign in to comment.