Fix size of highlighting if frontmatter is invalid (#2264)

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2022-08-01 18:49:31 +02:00 committed by GitHub
parent 7d53493f56
commit c50037fc9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 12 deletions

View file

@ -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'
})
})

View file

@ -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 [
{