-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
52 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |