2021-12-14 04:16:25 -05:00
|
|
|
/*
|
2022-07-21 16:36:46 -04:00
|
|
|
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
2021-12-14 04:16:25 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { useMemo } from 'react'
|
|
|
|
import { useApplicationState } from './use-application-state'
|
|
|
|
|
2021-12-25 10:44:24 -05:00
|
|
|
/**
|
|
|
|
* Returns the markdown content from the global application state trimmed to the maximal note length and without the frontmatter lines.
|
2022-07-21 16:36:46 -04:00
|
|
|
*
|
|
|
|
* @return The array of markdown content lines
|
2021-12-25 10:44:24 -05:00
|
|
|
*/
|
2021-12-14 04:16:25 -05:00
|
|
|
export const useTrimmedNoteMarkdownContentWithoutFrontmatter = (): string[] => {
|
|
|
|
const maxLength = useApplicationState((state) => state.config.maxDocumentLength)
|
2022-05-11 06:47:58 -04:00
|
|
|
const markdownContent = useApplicationState((state) => ({
|
|
|
|
lines: state.noteDetails.markdownContent.lines,
|
|
|
|
content: state.noteDetails.markdownContent.plain
|
|
|
|
}))
|
2021-12-14 04:16:25 -05:00
|
|
|
const lineOffset = useApplicationState((state) => state.noteDetails.frontmatterRendererInfo.lineOffset)
|
|
|
|
|
|
|
|
const trimmedLines = useMemo(() => {
|
2021-12-25 10:44:24 -05:00
|
|
|
if (markdownContent.content.length > maxLength) {
|
|
|
|
return markdownContent.content.slice(0, maxLength).split('\n')
|
2021-12-14 04:16:25 -05:00
|
|
|
} else {
|
2021-12-25 10:44:24 -05:00
|
|
|
return markdownContent.lines
|
2021-12-14 04:16:25 -05:00
|
|
|
}
|
2021-12-25 10:44:24 -05:00
|
|
|
}, [markdownContent, maxLength])
|
2021-12-14 04:16:25 -05:00
|
|
|
|
|
|
|
return useMemo(() => {
|
|
|
|
return trimmedLines.slice(lineOffset)
|
|
|
|
}, [lineOffset, trimmedLines])
|
|
|
|
}
|