Skip to content

Commit

Permalink
fix: 🐛 fix bug in parsing links
Browse files Browse the repository at this point in the history
  • Loading branch information
themashcodee committed Aug 26, 2024
1 parent 405350c commit 4c7240d
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"repository": "https://github.com/themashcodee/slack-blocks-to-jsx.git",
"license": "MIT",
"version": "0.5.6",
"version": "0.5.7",
"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
Expand Down
2 changes: 2 additions & 0 deletions src/utils/markdown_parser/elements/paragraph.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Delete,
Emphasis,
HTML,
InlineCode,
Link,
SlackBroadcast,
Expand All @@ -25,6 +26,7 @@ export const Paragraph = (props: Props) => {
<p>
{element.children.map((subelement, i) => {
if (subelement.type === "text") return <Text key={i} element={subelement} />;
if (subelement.type === "html") return <HTML key={i} element={subelement} />;
if (subelement.type === "emphasis") return <Emphasis key={i} element={subelement} />;
if (subelement.type === "inlineCode") return <InlineCode key={i} element={subelement} />;
if (subelement.type === "delete") return <Delete key={i} element={subelement} />;
Expand Down
22 changes: 21 additions & 1 deletion src/utils/markdown_parser/parser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ type Options = {
hooks: GlobalStore["hooks"];
};

// Helper function to check if a string is a valid URL
function isValidURL(string: string) {
try {
new URL(string);
return true;
} catch (_) {
return false;
}
}

export const markdown_parser = (markdown: string, options: Options): ReactNode => {
if (!markdown) return null;

Expand All @@ -40,7 +50,16 @@ export const markdown_parser = (markdown: string, options: Options): ReactNode =
// REPLACE SINGLE tilde WITH DOUBLE tilde
text_string = text_string.replace(/(?<!~)~(?!~)([^~]+)~(?!~)/g, "~~$1~~");
// CHANGE LINK FORMATTING FROM <link|label> to [label](link), EXCLUDING LINKS STARTING WITH !date
text_string = text_string.replace(/<(?!(?:!date))([^|>]+)\|([^>]+)>/g, "[$2]($1)");
text_string = text_string.replace(/<(?!(?:!date))([^|>]+)\|([^>]+)>/g, (match, link, label) => {
if (isValidURL(link)) return `[${label}](${link})`;
return match;
});
// CHANGE LINK FORMATTING FROM <link> to [link](link), EXCLUDING LINKS STARTING WITH !date
text_string = text_string.replace(/<(?!(?:!date))([^|>]+)>/g, (match, link) => {
if (isValidURL(link)) return `[${link}](${link})`;
return match;
});

// REPLACE \n\n WITH '[[DOUBLE_LINE_BREAK]]' to prevent @yozora/parser to eat it
text_string = text_string.replace(/\n\n/g, "[[DOUBLE_LINE_BREAK]]");
// REPLACE <!here> with @here
Expand All @@ -51,6 +70,7 @@ export const markdown_parser = (markdown: string, options: Options): ReactNode =
text_string = text_string.replace(/<!channel>/g, "@channel");

const parsed_data = parser.parse(text_string);
console.log({ text_string, parsed_data });

const elements = parsed_data.children as unknown as MarkdownElement[];

Expand Down
25 changes: 25 additions & 0 deletions src/utils/markdown_parser/sub_elements/html.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { HTMLSubElement } from "../types";

type Props = {
element: HTMLSubElement;
};

export const HTML = (props: Props) => {
const { element } = props;

if (!element.value) return <span>{element.value}</span>;
if (element.value === " ") return <span>&nbsp;</span>;

return (
<span>
{element.value.split("[[DOUBLE_LINE_BREAK]]").map((line, index) => {
return (
<span key={index}>
{index > 0 && <span className="slack_blocks_to_jsx__line_break_not_first"></span>}
{line}
</span>
);
})}
</span>
);
};
1 change: 1 addition & 0 deletions src/utils/markdown_parser/sub_elements/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from "./slack_user_group_mention";
export * from "./slack_broadcast";
export * from "./slack_date";
export * from "./slack_emoji";
export * from "./html";
6 changes: 6 additions & 0 deletions src/utils/markdown_parser/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ export type SlackEmojiSubElement = {
value: string;
};

export type HTMLSubElement = {
type: "html";
value: string;
};

export type SlackDateSubElement = {
type: "slack_date";
value: {
Expand Down Expand Up @@ -114,6 +119,7 @@ export type ParagraphElement = {
| SlackBroadcastSubElement
| SlackDateSubElement
| SlackEmojiSubElement
| HTMLSubElement
)[];
};

Expand Down

0 comments on commit 4c7240d

Please sign in to comment.