Skip to content
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

v0.5.1 피드백 반영 작업 #322

Merged
merged 3 commits into from
Dec 15, 2022
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
6 changes: 2 additions & 4 deletions frontend/components/common/Content/styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const ContentBody = styled.div`
> * {
line-height: 2;
font-weight: 400;
word-break: break-word;
}

h1,
Expand Down Expand Up @@ -56,12 +57,9 @@ export const ContentBody = styled.div`

p {
img {
max-width: 720px;
max-width: 480px;

@media ${(props) => props.theme.desktop} {
width: 500px;
}
@media ${(props) => props.theme.tablet} {
width: 100%;
}
}
Expand Down
14 changes: 7 additions & 7 deletions frontend/components/viewer/TOC/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import useBookmark from '@hooks/useBookmark';
import { IBookScraps } from '@interfaces';
import { TextMedium, TextSmall } from '@styles/common';
import { FlexCenter, FlexSpaceBetween } from '@styles/layout';
import { text2link } from '@utils/toc';

import {
TocWrapper,
Expand All @@ -32,8 +31,9 @@ import {
interface TocProps {
articleId: number;
articleToc: {
title: string;
count: number | undefined;
heading: string;
link: string;
padding: number;
}[];
book: IBookScraps;
isOpen: boolean;
Expand Down Expand Up @@ -94,11 +94,11 @@ export default function TOC({
{isArticleShown &&
articleToc.map((article) => (
<TocArticleTitle
href={text2link(article.title).toLowerCase()}
key={article.title}
count={article.count}
href={article.link}
key={article.link}
padding={article.padding}
>
{article.title}
{article.heading}
</TocArticleTitle>
))}
</TocCurrentArticle>
Expand Down
5 changes: 3 additions & 2 deletions frontend/components/viewer/TOC/styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,14 @@ export const TocCurrentText = styled(TextSmall)`
cursor: 'pointer';
font-weight: 600;
`;
export const TocArticleTitle = styled(Link)<{ count: number | undefined }>`

export const TocArticleTitle = styled(Link)<{ padding: number }>`
font-size: 14px;
line-height: 20px;
text-decoration: none;
display: block;
margin-bottom: 5px;
padding-left: ${(props) => `${props.count}px`};
padding-left: ${(props) => `${props.padding}px`};

&:hover {
color: var(--primary-color);
Expand Down
39 changes: 0 additions & 39 deletions frontend/utils/articleConversion.ts

This file was deleted.

16 changes: 5 additions & 11 deletions frontend/utils/highlight-keyword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const getFirstKeyword = (text: string, keywords: string[]) => {
keywords.forEach((keyword) => {
const index = text.toLowerCase().indexOf(keyword.toLowerCase());

if (index !== -1) keywordMap.set(index, keyword);
if (index !== -1) keywordMap.set(index, text.slice(index, index + keyword.length));
});

if (keywordMap.size === 0) return { keyword: '', index: -1, validKeywords: [] };
Expand All @@ -24,27 +24,21 @@ export const getTextAfterLastNewLine = (text: string, keywords: string[]) => {

const newLineIndex = text.slice(0, index).lastIndexOf('\n');

return newLineIndex === -1 ? text : text.slice(newLineIndex);
return newLineIndex === -1 ? text.slice(0, 200) : text.slice(newLineIndex, newLineIndex + 200);
};

export const highlightKeyword = (text: string, keywords: string[], length = 0): React.ReactNode => {
if (length > 200) return '';

export const highlightKeyword = (text: string, keywords: string[]): React.ReactNode => {
const { keyword, index, validKeywords } = getFirstKeyword(text, keywords);

if (index === -1) return text;

const endIndex = index + keyword.length;

const affixText = text.slice(0, index);

const accumulatedLength = length + affixText.length + keyword.length;

return (
<>
{affixText}
{text.slice(0, index)}
<b>{keyword}</b>
{highlightKeyword(text.slice(endIndex), validKeywords, accumulatedLength)}
{highlightKeyword(text.slice(endIndex), validKeywords)}
</>
);
};
45 changes: 25 additions & 20 deletions frontend/utils/toc.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
export const parseHeadings = (content: string) => {
// 게시물 본문을 줄바꿈 기준으로 나누고, 제목 요소인 것만 저장
const headings = content.split('\n').filter((line) => line.includes('# '));

// 예외처리 - 제목은 문자열 시작부터 #을 써야함
const parsedHeadings = headings
.filter((heading) => heading.startsWith('#'))
.map((heading) => {
// #의 갯수에 따라 제목의 크기가 달라지므로 갯수를 센다.
let count = heading.match(/#/g)?.length;

// 갯수에 따라 목차에 그릴때 들여쓰기 하기위해 *10을 함.
if (count) count *= 16;
export const text2link = (text: string) => {
return `#${text
.replace(/ /g, '-')
.replace(/[^\uAC00-\uD7A30-9a-zA-Z_-]/g, '')
.toLowerCase()}`;
};

// 제목의 내용물만 꺼내기 위해 '# '을 기준으로 나누고, 백틱과 공백을 없애주고 count와 묶어서 리턴
export const parseHeadings = (content: string) => {
const headings = content
.split('\n')
.filter((line) => /^\s*#{1,3}/m.test(line))
.map((line) => {
return {
title: heading.split('# ')[1].trim(),
count,
text: line.trim().split('# ')[1],
length: line.match(/#/g)?.length || 0,
};
});

return parsedHeadings;
};
const parsedHeadings = headings.map(({ text, length }, index) => {
const currHeadingIndex = headings
.slice(0, index)
.reduce((count, { text: currText }) => (currText === text ? count + 1 : count), 0);

export const text2link = (text: string) => {
return `#${text.replace(/ /g, '-').replace(/[^\uAC00-\uD7A30-9a-zA-Z_-]/g, '')}`;
return {
heading: text,
link: `${text2link(text)}${currHeadingIndex ? `-${currHeadingIndex}` : ``}`,
padding: length * 16,
};
});

return parsedHeadings;
};