-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
feat(helper/toc): specify maximum number of items to output #5487
Merged
+162
−1
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f00bf65
feat(helper/toc): specify maximum number of items to output
KentarouTakeda c17db64
perf: skip evaluation if `max_items` is not specified
KentarouTakeda f8d04a6
perf: streamline getting min and max heading level
KentarouTakeda 2507aa2
Merge branch 'master' into max-items-of-toc
uiolee 628b975
Merge branch 'master' into max-items-of-toc
yoshinorin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ import { tocObj, escapeHTML, encodeURL } from 'hexo-util'; | |
interface Options { | ||
min_depth?: number; | ||
max_depth?: number; | ||
max_items?: number; | ||
class?: string; | ||
class_item?: string; | ||
class_link?: string; | ||
|
@@ -17,6 +18,7 @@ function tocHelper(str: string, options: Options = {}) { | |
options = Object.assign({ | ||
min_depth: 1, | ||
max_depth: 6, | ||
max_items: Infinity, | ||
class: 'toc', | ||
class_item: '', | ||
class_link: '', | ||
|
@@ -27,7 +29,7 @@ function tocHelper(str: string, options: Options = {}) { | |
list_number: true | ||
}, options); | ||
|
||
const data = tocObj(str, { min_depth: options.min_depth, max_depth: options.max_depth }); | ||
const data = getAndTruncateTocObj(str, { min_depth: options.min_depth, max_depth: options.max_depth }, options.max_items); | ||
|
||
if (!data.length) return ''; | ||
|
||
|
@@ -102,4 +104,27 @@ function tocHelper(str: string, options: Options = {}) { | |
return result; | ||
} | ||
|
||
function getAndTruncateTocObj(str: string, options: {min_depth: number, max_depth: number}, max_items: number) { | ||
let data = tocObj(str, { min_depth: options.min_depth, max_depth: options.max_depth }); | ||
|
||
if (data.length === 0) { | ||
return data; | ||
} | ||
if (max_items < 1 || max_items === Infinity) { | ||
return data; | ||
} | ||
|
||
const levels = data.map(item => item.level); | ||
const min = Math.min(...levels); | ||
const max = Math.max(...levels); | ||
|
||
for (let currentLevel = max; data.length > max_items && currentLevel > min; currentLevel--) { | ||
data = data.filter(item => item.level < currentLevel); | ||
} | ||
Comment on lines
+121
to
+123
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be inefficient (iterating data over and over again). But I don't have any better idea for now. |
||
|
||
data = data.slice(0, max_items); | ||
SukkaW marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return data; | ||
} | ||
|
||
export = tocHelper; |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the user does not need
max_items
, then there is no need to perform this operation everytime.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed: c17db64
I thought it was necessary to avoid
tocObj()
appearing multiple times in different places, so I wrote the skip judgment at the beginning ofgetAndTruncateTocObj()
instead of where you gave the example.