Skip to content

Force links to open in new tab #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import React, { memo, forwardRef, useState } from "react";
import { Warning, CaretDown } from "@phosphor-icons/react";
import renderMarkdown from "@/utils/chat/markdown";
import DOMPurify from "@/utils/chat/purify";
import { embedderSettings } from "@/main";
import { v4 } from "uuid";
import createDOMPurify from "dompurify";
import AnythingLLMIcon from "@/assets/anything-llm-icon.svg";
import { formatDate } from "@/utils/date";

const DOMPurify = createDOMPurify(window);

const ThoughtBubble = ({ thought }) => {
const [isExpanded, setIsExpanded] = useState(false);
if (!thought || !embedderSettings.settings.showThoughts) return null;
Expand Down
7 changes: 7 additions & 0 deletions src/utils/chat/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ const markdown = markdownIt({
// Enable <ol> and <ul> items to not assume an HTML structure so we can keep numbering from responses.
.disable("list");

// Custom renderer rule to make links open in new tab
markdown.renderer.rules.link_open = (tokens, idx) => {
const token = tokens[idx];
const href = token.attrs.find((attr) => attr[0] === "href");
return `<a href="${href[1]}" target="_blank" rel="noopener noreferrer">`;
};

export default function renderMarkdown(text = "") {
return markdown.render(text);
}
8 changes: 8 additions & 0 deletions src/utils/chat/purify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import createDOMPurify from "dompurify";

const DOMPurify = createDOMPurify(window);
DOMPurify.setConfig({
ADD_ATTR: ["target", "rel"],
});

export default DOMPurify;