{
- const markdownContent = useNoteMarkdownContentWithoutFrontmatter()
+ const markdownContentLines = useTrimmedNoteMarkdownContentWithoutFrontmatter()
useTranslation()
useSendFrontmatterInfoFromReduxToRenderer()
@@ -21,7 +21,7 @@ export const SlideShowPageContent: React.FC = () => {
diff --git a/src/hooks/common/use-note-markdown-content-without-frontmatter.ts b/src/hooks/common/use-note-markdown-content-without-frontmatter.ts
deleted file mode 100644
index edb2940ef..000000000
--- a/src/hooks/common/use-note-markdown-content-without-frontmatter.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
- *
- * SPDX-License-Identifier: AGPL-3.0-only
- */
-
-import { useNoteMarkdownContent } from './use-note-markdown-content'
-import { useApplicationState } from './use-application-state'
-import { useMemo } from 'react'
-
-/**
- * Extracts the markdown content of the current note from the global application state and removes the frontmatter.
- * @return the markdown content of the note without frontmatter
- */
-export const useNoteMarkdownContentWithoutFrontmatter = (): string => {
- const markdownContent = useNoteMarkdownContent()
- const lineOffset = useApplicationState((state) => state.noteDetails.frontmatterRendererInfo.lineOffset)
-
- return useMemo(() => markdownContent.split('\n').slice(lineOffset).join('\n'), [markdownContent, lineOffset])
-}
diff --git a/src/hooks/common/use-trimmed-note-markdown-content-without-frontmatter.ts b/src/hooks/common/use-trimmed-note-markdown-content-without-frontmatter.ts
new file mode 100644
index 000000000..65a0f818c
--- /dev/null
+++ b/src/hooks/common/use-trimmed-note-markdown-content-without-frontmatter.ts
@@ -0,0 +1,28 @@
+/*
+ * SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
+ *
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+
+import { useMemo } from 'react'
+import { useApplicationState } from './use-application-state'
+import { useNoteMarkdownContent } from './use-note-markdown-content'
+
+export const useTrimmedNoteMarkdownContentWithoutFrontmatter = (): string[] => {
+ const maxLength = useApplicationState((state) => state.config.maxDocumentLength)
+ const markdownContent = useNoteMarkdownContent()
+ const markdownContentLines = useApplicationState((state) => state.noteDetails.markdownContentLines)
+ const lineOffset = useApplicationState((state) => state.noteDetails.frontmatterRendererInfo.lineOffset)
+
+ const trimmedLines = useMemo(() => {
+ if (markdownContent.length > maxLength) {
+ return markdownContent.slice(0, maxLength).split('\n')
+ } else {
+ return markdownContentLines
+ }
+ }, [markdownContent, markdownContentLines, maxLength])
+
+ return useMemo(() => {
+ return trimmedLines.slice(lineOffset)
+ }, [lineOffset, trimmedLines])
+}
diff --git a/src/redux/note-details/frontmatter-extractor/extractor.test.ts b/src/redux/note-details/frontmatter-extractor/extractor.test.ts
index 9cc20df37..a070091d7 100644
--- a/src/redux/note-details/frontmatter-extractor/extractor.test.ts
+++ b/src/redux/note-details/frontmatter-extractor/extractor.test.ts
@@ -10,47 +10,47 @@ import type { PresentFrontmatterExtractionResult } from './types'
describe('frontmatter extraction', () => {
describe('isPresent property', () => {
it('is false when note does not contain three dashes at all', () => {
- const testNote = 'abcdef\nmore text'
+ const testNote = ['abcdef', 'more text']
const extraction = extractFrontmatter(testNote)
expect(extraction.isPresent).toBe(false)
})
it('is false when note does not start with three dashes', () => {
- const testNote = '\n---\nthis is not frontmatter'
+ const testNote = ['', '---', 'this is not frontmatter']
const extraction = extractFrontmatter(testNote)
expect(extraction.isPresent).toBe(false)
})
it('is false when note start with less than three dashes', () => {
- const testNote = '--\nthis is not frontmatter'
+ const testNote = ['--', 'this is not frontmatter']
const extraction = extractFrontmatter(testNote)
expect(extraction.isPresent).toBe(false)
})
it('is false when note starts with three dashes but contains other characters in the same line', () => {
- const testNote = '--- a\nthis is not frontmatter'
+ const testNote = ['--- a', 'this is not frontmatter']
const extraction = extractFrontmatter(testNote)
expect(extraction.isPresent).toBe(false)
})
it('is false when note has no ending marker for frontmatter', () => {
- const testNote = '---\nthis is not frontmatter\nbecause\nthere is no\nend marker'
+ const testNote = ['---', 'this is not frontmatter', 'because', 'there is no', 'end marker']
const extraction = extractFrontmatter(testNote)
expect(extraction.isPresent).toBe(false)
})
it('is false when note end marker is present but with not the same amount of dashes as start marker', () => {
- const testNote = '---\nthis is not frontmatter\n----\ncontent'
+ const testNote = ['---', 'this is not frontmatter', '----', 'content']
const extraction = extractFrontmatter(testNote)
expect(extraction.isPresent).toBe(false)
})
it('is true when note end marker is present with the same amount of dashes as start marker', () => {
- const testNote = '---\nthis is frontmatter\n---\ncontent'
+ const testNote = ['---', 'this is frontmatter', '---', 'content']
const extraction = extractFrontmatter(testNote)
expect(extraction.isPresent).toBe(true)
})
it('is true when note end marker is present with the same amount of dashes as start marker but without content', () => {
- const testNote = '---\nthis is frontmatter\n---'
+ const testNote = ['---', 'this is frontmatter', '---']
const extraction = extractFrontmatter(testNote)
expect(extraction.isPresent).toBe(true)
})
it('is true when note end marker is present with the same amount of dots as start marker', () => {
- const testNote = '---\nthis is frontmatter\n...\ncontent'
+ const testNote = ['---', 'this is frontmatter', '...', 'content']
const extraction = extractFrontmatter(testNote)
expect(extraction.isPresent).toBe(true)
})
@@ -58,22 +58,22 @@ describe('frontmatter extraction', () => {
describe('lineOffset property', () => {
it('is correct for single line frontmatter without content', () => {
- const testNote = '---\nsingle line frontmatter\n...'
+ const testNote = ['---', 'single line frontmatter', '...']
const extraction = extractFrontmatter(testNote) as PresentFrontmatterExtractionResult
expect(extraction.lineOffset).toEqual(3)
})
it('is correct for single line frontmatter with content', () => {
- const testNote = '---\nsingle line frontmatter\n...\ncontent'
+ const testNote = ['---', 'single line frontmatter', '...', 'content']
const extraction = extractFrontmatter(testNote) as PresentFrontmatterExtractionResult
expect(extraction.lineOffset).toEqual(3)
})
it('is correct for multi-line frontmatter without content', () => {
- const testNote = '---\nabc\n123\ndef\n...'
+ const testNote = ['---', 'abc', '123', 'def', '...']
const extraction = extractFrontmatter(testNote) as PresentFrontmatterExtractionResult
expect(extraction.lineOffset).toEqual(5)
})
it('is correct for multi-line frontmatter with content', () => {
- const testNote = '---\nabc\n123\ndef\n...\ncontent'
+ const testNote = ['---', 'abc', '123', 'def', '...', 'content']
const extraction = extractFrontmatter(testNote) as PresentFrontmatterExtractionResult
expect(extraction.lineOffset).toEqual(5)
})
@@ -81,12 +81,12 @@ describe('frontmatter extraction', () => {
describe('rawText property', () => {
it('contains single-line frontmatter text', () => {
- const testNote = '---\nsingle-line\n...\ncontent'
+ const testNote = ['---', 'single-line', '...', 'content']
const extraction = extractFrontmatter(testNote) as PresentFrontmatterExtractionResult
expect(extraction.rawText).toEqual('single-line')
})
it('contains multi-line frontmatter text', () => {
- const testNote = '---\nmulti\nline\n...\ncontent'
+ const testNote = ['---', 'multi', 'line', '...', 'content']
const extraction = extractFrontmatter(testNote) as PresentFrontmatterExtractionResult
expect(extraction.rawText).toEqual('multi\nline')
})
diff --git a/src/redux/note-details/frontmatter-extractor/extractor.ts b/src/redux/note-details/frontmatter-extractor/extractor.ts
index 0cc7f4a18..e5f3e6be4 100644
--- a/src/redux/note-details/frontmatter-extractor/extractor.ts
+++ b/src/redux/note-details/frontmatter-extractor/extractor.ts
@@ -12,14 +12,13 @@ const FRONTMATTER_END_REGEX = /^(?:-{3,}|\.{3,})$/
* Extracts a frontmatter block from a given multiline string.
* A valid frontmatter block requires the content to start with a line containing at least three dashes.
* The block is terminated by a line containing the same amount of dashes or dots as the first line.
- * @param content The multiline string from which the frontmatter should be extracted.
+ * @param lines The lines from which the frontmatter should be extracted.
* @return { isPresent } false if no frontmatter block could be found, true if a block was found.
* { rawFrontmatterText } if a block was found, this property contains the extracted text without the fencing.
* { frontmatterLines } if a block was found, this property contains the number of lines to skip from the
* given multiline string for retrieving the non-frontmatter content.
*/
-export const extractFrontmatter = (content: string): FrontmatterExtractionResult => {
- const lines = content.split('\n')
+export const extractFrontmatter = (lines: string[]): FrontmatterExtractionResult => {
if (lines.length < 2 || !FRONTMATTER_BEGIN_REGEX.test(lines[0])) {
return {
isPresent: false
diff --git a/src/redux/note-details/initial-state.ts b/src/redux/note-details/initial-state.ts
index 6d3778678..bc8861014 100644
--- a/src/redux/note-details/initial-state.ts
+++ b/src/redux/note-details/initial-state.ts
@@ -19,6 +19,7 @@ export const initialSlideOptions: SlideOptions = {
export const initialState: NoteDetails = {
markdownContent: '',
+ markdownContentLines: [],
rawFrontmatter: '',
frontmatterRendererInfo: {
frontmatterInvalid: false,
diff --git a/src/redux/note-details/reducer.ts b/src/redux/note-details/reducer.ts
index c779b1cd6..65a8d6bab 100644
--- a/src/redux/note-details/reducer.ts
+++ b/src/redux/note-details/reducer.ts
@@ -74,7 +74,7 @@ const buildStateFromTaskListUpdate = (
changedLine: number,
checkboxChecked: boolean
): NoteDetails => {
- const lines = state.markdownContent.split('\n')
+ const lines = state.markdownContentLines
const results = TASK_REGEX.exec(lines[changedLine])
if (results) {
const before = results[1]
@@ -88,23 +88,26 @@ const buildStateFromTaskListUpdate = (
/**
* Builds a {@link NoteDetails} redux state from a fresh document content.
* @param state The previous redux state.
- * @param markdownContent The fresh document content consisting of the frontmatter and markdown part.
+ * @param newMarkdownContent The fresh document content consisting of the frontmatter and markdown part.
* @return An updated {@link NoteDetails} redux state.
*/
-const buildStateFromMarkdownContentUpdate = (state: NoteDetails, markdownContent: string): NoteDetails => {
- const frontmatterExtraction = extractFrontmatter(markdownContent)
+const buildStateFromMarkdownContentUpdate = (state: NoteDetails, newMarkdownContent: string): NoteDetails => {
+ const markdownContentLines = newMarkdownContent.split('\n')
+ const frontmatterExtraction = extractFrontmatter(markdownContentLines)
if (frontmatterExtraction.isPresent) {
return buildStateFromFrontmatterUpdate(
{
...state,
- markdownContent: markdownContent
+ markdownContent: newMarkdownContent,
+ markdownContentLines: markdownContentLines
},
frontmatterExtraction
)
} else {
return {
...state,
- markdownContent: markdownContent,
+ markdownContent: newMarkdownContent,
+ markdownContentLines: markdownContentLines,
rawFrontmatter: '',
noteTitle: generateNoteTitle(initialState.frontmatter, state.firstHeading),
frontmatter: initialState.frontmatter,
@@ -193,6 +196,7 @@ const generateNoteTitle = (frontmatter: NoteFrontmatter, firstHeading?: string)
const convertNoteDtoToNoteDetails = (note: NoteDto): NoteDetails => {
return {
markdownContent: note.content,
+ markdownContentLines: note.content.split('\n'),
rawFrontmatter: '',
frontmatterRendererInfo: initialState.frontmatterRendererInfo,
frontmatter: initialState.frontmatter,
diff --git a/src/redux/note-details/types/note-details.ts b/src/redux/note-details/types/note-details.ts
index 4052d7bf2..b3d197204 100644
--- a/src/redux/note-details/types/note-details.ts
+++ b/src/redux/note-details/types/note-details.ts
@@ -13,6 +13,7 @@ import type { ISO6391 } from './iso6391'
*/
export interface NoteDetails {
markdownContent: string
+ markdownContentLines: string[]
rawFrontmatter: string
frontmatter: NoteFrontmatter
frontmatterRendererInfo: RendererFrontmatterInfo