Skip to content

Commit

Permalink
feat: show links in markdown content as chips
Browse files Browse the repository at this point in the history
  • Loading branch information
drodil committed Nov 29, 2024
1 parent 37b327f commit eb156c7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
11 changes: 11 additions & 0 deletions plugins/qeta-common/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { parseEntityRef, stringifyEntityRef } from '@backstage/catalog-model';
import { compact } from 'lodash';
import { isValidTag } from './tags';

export const truncate = (str: string, n: number): string => {
return str.length > n ? `${str.slice(0, n - 1)}...` : str;
Expand All @@ -23,6 +24,16 @@ export const findUserMentions = (text: string): string[] => {
);
};

export const findTagMentions = (text: string): string[] => {
const mentions = text.match(/#(\S+)/g);
const ret = mentions ? Array.from(new Set(mentions)) : [];
return compact(
ret.filter(tag => {
return isValidTag(tag);
}),
);
};

// Covers many common but not all cases of markdown formatting
export const removeMarkdownFormatting = (text: string): string => {
// Remove HTML tags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import {
a11yLight,
} from 'react-syntax-highlighter/dist/esm/styles/hljs';
import { IconButton, makeStyles, Tooltip, Typography } from '@material-ui/core';
import { findUserMentions } from '@drodil/backstage-plugin-qeta-common';
import {
findTagMentions,
findUserMentions,
} from '@drodil/backstage-plugin-qeta-common';
import gfm from 'remark-gfm';
import rehypeSlug from 'rehype-slug';
import rehypeToc, { HeadingNode, TextNode } from '@jsdevtools/rehype-toc';
Expand All @@ -20,6 +23,7 @@ import { Variant } from '@material-ui/core/styles/createTypography';
import GithubSlugger from 'github-slugger';
import { HtmlElementNode } from '@jsdevtools/rehype-toc/lib/types';
import { find } from 'unist-util-find';
import { TagChip } from '../TagsAndEntities/TagChip';

const slugger = new GithubSlugger();

Expand Down Expand Up @@ -303,20 +307,34 @@ export const MarkdownRenderer = (props: {
if (typeof child !== 'string') {
return child;
}
const mentions = findUserMentions(child);
if (mentions.length === 0) {
const userMentions = findUserMentions(child);
const tagMentions = findTagMentions(child);
if (userMentions.length === 0 && tagMentions.length === 0) {
return child;
}

return child.split(' ').map((word: string) => {
const mention = mentions.find(m => word.includes(m));
if (mention) {
const userMention = userMentions.find(m => word === m);
if (userMention) {
return (
<>
<EntityRefLink
entityRef={userMention.slice(1)}
hideIcon
/>{' '}
</>
);
}

const tagMention = tagMentions.find(m => word === m);
if (tagMention) {
return (
<>
<EntityRefLink entityRef={mention.slice(1)} hideIcon />{' '}
<TagChip tag={tagMention.slice(1)} />
</>
);
}

return <>{word} </>;
});
});
Expand Down
10 changes: 9 additions & 1 deletion plugins/qeta-react/src/components/TagsAndEntities/TagChip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ const TagTooltip = (props: { tag: string }) => {
if (res) {
cache.set(tag, res);
setResp(res);
} else {
setResp({
id: 0,
tag,
postsCount: 0,
followerCount: 0,
description: '',
});
}
});
}, [qetaApi, tag]);
Expand All @@ -54,7 +62,7 @@ const TagTooltip = (props: { tag: string }) => {
<MarkdownRenderer content={resp.description} />
</Grid>
)}
{!tags.loading && (
{!tags.loading && resp.id !== 0 && (
<Grid item xs={12}>
<Button
size="small"
Expand Down

0 comments on commit eb156c7

Please sign in to comment.