Skip to content

Commit

Permalink
fix: fix anidb description… again (#666)
Browse files Browse the repository at this point in the history
* fix: fix anidb description… again

- add missing link templates to regex, since we only accounted for short
  links, but people also insert long links,

- merge multi spaces into single space,

- remove summary credits, and

- account for people's disability to spell correctly.

* fix: fix tag descriptions
  • Loading branch information
revam authored Nov 3, 2023
1 parent 2b8088c commit 5ae6042
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/components/Collection/AnidbDescription.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
import React, { useMemo } from 'react';

const RemoveSummaryRegex = /^\n(Source|Note|Summary):.*/mg;
// The question marks are there because people can't spell…
const RemoveSummaryRegex = /\b(Sour?ce|Note|Summ?ary):([^\r\n]+|$)/mg;

const MultiSpacesRegex = /\s{2,}/g;

const CleanMiscLinesRegex = /^(\*|--|~) /sg;

const CleanMultiEmptyLinesRegex = /\n{2,}/sg;
const LinkRegex = /(?<url>http:\/\/anidb\.net\/(?<type>ch|cr|[feat])(?<id>\d+)) \[(?<text>[^\]]+)]/g;

// eslint-disable-next-line operator-linebreak -- Because dprint and eslint can't agree otherwise. Feel free to fix it.
const LinkRegex =
/(?<url>http:\/\/anidb\.net\/(?<type>ch|cr|[feat]|(?:character|creator|file|episode|anime|tag)\/)(?<id>\d+)) \[(?<text>[^\]]+)]/g;

const AnidbDescription = ({ text }: { text: string }) => {
const modifiedText = useMemo(() => {
const cleanedText = text
.replaceAll(CleanMiscLinesRegex, '')
.replaceAll(RemoveSummaryRegex, '')
.replaceAll(CleanMultiEmptyLinesRegex, '\n');
.replaceAll(CleanMultiEmptyLinesRegex, '\n')
.replaceAll(MultiSpacesRegex, ' ');

const lines = [] as React.ReactNode[];
let prevPos = 0;
Expand All @@ -21,7 +30,7 @@ const AnidbDescription = ({ text }: { text: string }) => {
lines.push(cleanedText.substring(prevPos, pos));
prevPos = pos + link[0].length;
lines.push(
link[4],
link.groups!.text,
);
link = LinkRegex.exec(cleanedText);
}
Expand Down
5 changes: 4 additions & 1 deletion src/pages/collection/series/SeriesTags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Icon } from '@mdi/react';
import cx from 'classnames';
import { map } from 'lodash';

import AnidbDescription from '@/components/Collection/AnidbDescription';
import Input from '@/components/Input/Input';
import ShokoPanel from '@/components/Panels/ShokoPanel';
import { useGetSeriesTagsQuery } from '@/core/rtkQuery/splitV3Api/seriesApi';
Expand All @@ -27,7 +28,9 @@ function SeriesTag(props: { item: TagType }) {
&nbsp;
<Icon path={isOpen ? mdiChevronUp : mdiChevronDown} size={1} />
</div>
<div className={cx('leading-5', { 'line-clamp-2': !isOpen })}>{item.Description}</div>
<div className={cx('leading-5', { 'line-clamp-2': !isOpen })}>
<AnidbDescription text={item?.Description || 'No description set.'} />
</div>
</div>
);
}
Expand Down

0 comments on commit 5ae6042

Please sign in to comment.