From c50037fc9fc18e323bd22d800fcfef94e6ffc997 Mon Sep 17 00:00:00 2001 From: Tilman Vatteroth Date: Mon, 1 Aug 2022 18:49:31 +0200 Subject: [PATCH] Fix size of highlighting if frontmatter is invalid (#2264) Signed-off-by: Tilman Vatteroth --- .../linter/frontmatter-linter.spec.ts | 16 ++++++++-------- .../editor-pane/linter/frontmatter-linter.ts | 9 +++++---- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/components/editor-page/editor-pane/linter/frontmatter-linter.spec.ts b/src/components/editor-page/editor-pane/linter/frontmatter-linter.spec.ts index ecbce1d42..0ca365259 100644 --- a/src/components/editor-page/editor-pane/linter/frontmatter-linter.spec.ts +++ b/src/components/editor-page/editor-pane/linter/frontmatter-linter.spec.ts @@ -45,8 +45,8 @@ describe('FrontmatterLinter', () => { testFrontmatterLinter( '---\ntags: a\n---', { - from: 4, - to: 11, + from: 5, + to: 12, severity: 'warning' }, 'tags:\n- a' @@ -56,8 +56,8 @@ describe('FrontmatterLinter', () => { testFrontmatterLinter( '---\ntags: 1\n---', { - from: 4, - to: 11, + from: 5, + to: 12, severity: 'warning' }, 'tags:\n- 1' @@ -67,8 +67,8 @@ describe('FrontmatterLinter', () => { testFrontmatterLinter( '---\ntags: 123, a\n---', { - from: 4, - to: 16, + from: 5, + to: 17, severity: 'warning' }, 'tags:\n- 123\n- a' @@ -77,8 +77,8 @@ describe('FrontmatterLinter', () => { }) it('with invalid yaml', () => { testFrontmatterLinter('---\n1\n 2: 3\n---', { - from: 0, - to: 16, + from: 4, + to: 12, severity: 'error' }) }) diff --git a/src/components/editor-page/editor-pane/linter/frontmatter-linter.ts b/src/components/editor-page/editor-pane/linter/frontmatter-linter.ts index ad24df013..1d917fb16 100644 --- a/src/components/editor-page/editor-pane/linter/frontmatter-linter.ts +++ b/src/components/editor-page/editor-pane/linter/frontmatter-linter.ts @@ -23,13 +23,14 @@ export class FrontmatterLinter implements Linter { if (!frontmatterExtraction.isPresent) { return [] } - const frontmatterLines = lines.slice(0, frontmatterExtraction.lineOffset + 1) + const startOfYaml = lines[0].length + 1 + const frontmatterLines = lines.slice(1, frontmatterExtraction.lineOffset - 1) const rawNoteFrontmatter = FrontmatterLinter.loadYaml(frontmatterExtraction.rawText) if (rawNoteFrontmatter === undefined) { return [ { - from: 0, - to: frontmatterLines.join('\n').length, + from: startOfYaml, + to: startOfYaml + frontmatterLines.join('\n').length, message: t('editor.linter.frontmatter'), severity: 'error' } @@ -46,7 +47,7 @@ export class FrontmatterLinter implements Linter { const replacedText = 'tags:\n- ' + tags.join('\n- ') const tagsLineIndex = frontmatterLines.findIndex((value) => value.startsWith('tags: ')) const linesBeforeTagsLine = frontmatterLines.slice(0, tagsLineIndex) - const from = linesBeforeTagsLine.join('\n').length + 1 + const from = startOfYaml + linesBeforeTagsLine.join('\n').length + 1 const to = from + frontmatterLines[tagsLineIndex].length return [ {