mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-29 06:15:29 -04:00
Move frontmatter extraction from renderer to redux (#1413)
This commit is contained in:
parent
7fb7c55877
commit
04e16d8880
34 changed files with 680 additions and 589 deletions
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { extractFrontmatter } from './extract-frontmatter'
|
||||
import { PresentFrontmatterExtractionResult } from './types'
|
||||
|
||||
describe('frontmatter extraction', () => {
|
||||
describe('frontmatterPresent property', () => {
|
||||
it('is false when note does not contain three dashes at all', () => {
|
||||
const testNote = 'abcdef\nmore text'
|
||||
const extraction = extractFrontmatter(testNote)
|
||||
expect(extraction.frontmatterPresent).toBe(false)
|
||||
})
|
||||
it('is false when note does not start with three dashes', () => {
|
||||
const testNote = '\n---\nthis is not frontmatter'
|
||||
const extraction = extractFrontmatter(testNote)
|
||||
expect(extraction.frontmatterPresent).toBe(false)
|
||||
})
|
||||
it('is false when note start with less than three dashes', () => {
|
||||
const testNote = '--\nthis is not frontmatter'
|
||||
const extraction = extractFrontmatter(testNote)
|
||||
expect(extraction.frontmatterPresent).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 extraction = extractFrontmatter(testNote)
|
||||
expect(extraction.frontmatterPresent).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 extraction = extractFrontmatter(testNote)
|
||||
expect(extraction.frontmatterPresent).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 extraction = extractFrontmatter(testNote)
|
||||
expect(extraction.frontmatterPresent).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 extraction = extractFrontmatter(testNote)
|
||||
expect(extraction.frontmatterPresent).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 extraction = extractFrontmatter(testNote)
|
||||
expect(extraction.frontmatterPresent).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 extraction = extractFrontmatter(testNote)
|
||||
expect(extraction.frontmatterPresent).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('frontmatterLines property', () => {
|
||||
it('is correct for single line frontmatter without content', () => {
|
||||
const testNote = '---\nsingle line frontmatter\n...'
|
||||
const extraction = extractFrontmatter(testNote) as PresentFrontmatterExtractionResult
|
||||
expect(extraction.frontmatterLines).toEqual(3)
|
||||
})
|
||||
it('is correct for single line frontmatter with content', () => {
|
||||
const testNote = '---\nsingle line frontmatter\n...\ncontent'
|
||||
const extraction = extractFrontmatter(testNote) as PresentFrontmatterExtractionResult
|
||||
expect(extraction.frontmatterLines).toEqual(3)
|
||||
})
|
||||
it('is correct for multi-line frontmatter without content', () => {
|
||||
const testNote = '---\nabc\n123\ndef\n...'
|
||||
const extraction = extractFrontmatter(testNote) as PresentFrontmatterExtractionResult
|
||||
expect(extraction.frontmatterLines).toEqual(5)
|
||||
})
|
||||
it('is correct for multi-line frontmatter with content', () => {
|
||||
const testNote = '---\nabc\n123\ndef\n...\ncontent'
|
||||
const extraction = extractFrontmatter(testNote) as PresentFrontmatterExtractionResult
|
||||
expect(extraction.frontmatterLines).toEqual(5)
|
||||
})
|
||||
})
|
||||
|
||||
describe('rawFrontmatterText property', () => {
|
||||
it('contains single-line frontmatter text', () => {
|
||||
const testNote = '---\nsingle-line\n...\ncontent'
|
||||
const extraction = extractFrontmatter(testNote) as PresentFrontmatterExtractionResult
|
||||
expect(extraction.rawFrontmatterText).toEqual('single-line')
|
||||
})
|
||||
it('contains multi-line frontmatter text', () => {
|
||||
const testNote = '---\nmulti\nline\n...\ncontent'
|
||||
const extraction = extractFrontmatter(testNote) as PresentFrontmatterExtractionResult
|
||||
expect(extraction.rawFrontmatterText).toEqual('multi\nline')
|
||||
})
|
||||
})
|
||||
})
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { FrontmatterExtractionResult } from './types'
|
||||
|
||||
const FRONTMATTER_BEGIN_REGEX = /^-{3,}$/
|
||||
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.
|
||||
* @return { frontmatterPresent } 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')
|
||||
if (lines.length < 2 || !FRONTMATTER_BEGIN_REGEX.test(lines[0])) {
|
||||
return {
|
||||
frontmatterPresent: false
|
||||
}
|
||||
}
|
||||
for (let i = 1; i < lines.length; i++) {
|
||||
if (lines[i].length === lines[0].length && FRONTMATTER_END_REGEX.test(lines[i])) {
|
||||
return {
|
||||
frontmatterPresent: true,
|
||||
rawFrontmatterText: lines.slice(1, i).join('\n'),
|
||||
frontmatterLines: i + 1
|
||||
}
|
||||
}
|
||||
}
|
||||
return {
|
||||
frontmatterPresent: false
|
||||
}
|
||||
}
|
|
@ -0,0 +1,68 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
import { NoteFrontmatter } from './note-frontmatter'
|
||||
|
||||
describe('yaml frontmatter', () => {
|
||||
it('should parse "title"', () => {
|
||||
const noteFrontmatter = NoteFrontmatter.createFromYaml('title: test')
|
||||
expect(noteFrontmatter.title).toEqual('test')
|
||||
})
|
||||
|
||||
it('should parse "robots"', () => {
|
||||
const noteFrontmatter = NoteFrontmatter.createFromYaml('robots: index, follow')
|
||||
expect(noteFrontmatter.robots).toEqual('index, follow')
|
||||
})
|
||||
|
||||
it('should parse the deprecated tags syntax', () => {
|
||||
const noteFrontmatter = NoteFrontmatter.createFromYaml('tags: test123, abc')
|
||||
expect(noteFrontmatter.tags).toEqual(['test123', 'abc'])
|
||||
expect(noteFrontmatter.deprecatedTagsSyntax).toEqual(true)
|
||||
})
|
||||
|
||||
it('should parse the tags list syntax', () => {
|
||||
const noteFrontmatter = NoteFrontmatter.createFromYaml(`tags:
|
||||
- test123
|
||||
- abc
|
||||
`)
|
||||
expect(noteFrontmatter.tags).toEqual(['test123', 'abc'])
|
||||
expect(noteFrontmatter.deprecatedTagsSyntax).toEqual(false)
|
||||
})
|
||||
|
||||
it('should parse the tag inline-list syntax', () => {
|
||||
const noteFrontmatter = NoteFrontmatter.createFromYaml("tags: ['test123', 'abc']")
|
||||
expect(noteFrontmatter.tags).toEqual(['test123', 'abc'])
|
||||
expect(noteFrontmatter.deprecatedTagsSyntax).toEqual(false)
|
||||
})
|
||||
|
||||
it('should parse "breaks"', () => {
|
||||
const noteFrontmatter = NoteFrontmatter.createFromYaml('breaks: false')
|
||||
expect(noteFrontmatter.breaks).toEqual(false)
|
||||
})
|
||||
|
||||
it('should parse an empty opengraph object', () => {
|
||||
const noteFrontmatter = NoteFrontmatter.createFromYaml('opengraph:')
|
||||
expect(noteFrontmatter.opengraph).toEqual(new Map<string, string>())
|
||||
})
|
||||
|
||||
it('should parse an opengraph title', () => {
|
||||
const noteFrontmatter = NoteFrontmatter.createFromYaml(`opengraph:
|
||||
title: Testtitle
|
||||
`)
|
||||
expect(noteFrontmatter.opengraph.get('title')).toEqual('Testtitle')
|
||||
})
|
||||
|
||||
it('should parse multiple opengraph values', () => {
|
||||
const noteFrontmatter = NoteFrontmatter.createFromYaml(`opengraph:
|
||||
title: Testtitle
|
||||
image: https://dummyimage.com/48.png
|
||||
image:type: image/png
|
||||
`)
|
||||
expect(noteFrontmatter.opengraph.get('title')).toEqual('Testtitle')
|
||||
expect(noteFrontmatter.opengraph.get('image')).toEqual('https://dummyimage.com/48.png')
|
||||
expect(noteFrontmatter.opengraph.get('image:type')).toEqual('image/png')
|
||||
})
|
||||
})
|
70
src/components/common/note-frontmatter/note-frontmatter.ts
Normal file
70
src/components/common/note-frontmatter/note-frontmatter.ts
Normal file
|
@ -0,0 +1,70 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
// import { RevealOptions } from 'reveal.js'
|
||||
import { load } from 'js-yaml'
|
||||
import { ISO6391, NoteTextDirection, NoteType, RawNoteFrontmatter } from './types'
|
||||
|
||||
/**
|
||||
* Class that represents the parsed frontmatter metadata of a note.
|
||||
*/
|
||||
export class NoteFrontmatter {
|
||||
title: string
|
||||
description: string
|
||||
tags: string[]
|
||||
deprecatedTagsSyntax: boolean
|
||||
robots: string
|
||||
lang: typeof ISO6391[number]
|
||||
dir: NoteTextDirection
|
||||
breaks: boolean
|
||||
GA: string
|
||||
disqus: string
|
||||
type: NoteType
|
||||
opengraph: Map<string, string>
|
||||
|
||||
/**
|
||||
* Creates a new frontmatter metadata instance based on the given raw metadata properties.
|
||||
* @param rawData A {@link RawNoteFrontmatter} object containing the properties of the parsed yaml frontmatter.
|
||||
*/
|
||||
constructor(rawData: RawNoteFrontmatter) {
|
||||
this.title = rawData.title ?? ''
|
||||
this.description = rawData.description ?? ''
|
||||
this.robots = rawData.robots ?? ''
|
||||
this.breaks = rawData.breaks ?? true
|
||||
this.GA = rawData.GA ?? ''
|
||||
this.disqus = rawData.disqus ?? ''
|
||||
this.lang = (rawData.lang ? ISO6391.find((lang) => lang === rawData.lang) : undefined) ?? 'en'
|
||||
this.type =
|
||||
(rawData.type ? Object.values(NoteType).find((type) => type === rawData.type) : undefined) ?? NoteType.DOCUMENT
|
||||
this.dir =
|
||||
(rawData.dir ? Object.values(NoteTextDirection).find((dir) => dir === rawData.dir) : undefined) ??
|
||||
NoteTextDirection.LTR
|
||||
if (typeof rawData?.tags === 'string') {
|
||||
this.tags = rawData?.tags?.split(',').map((entry) => entry.trim()) ?? []
|
||||
this.deprecatedTagsSyntax = true
|
||||
} else if (typeof rawData?.tags === 'object') {
|
||||
this.tags = rawData?.tags?.filter((tag) => tag !== null) ?? []
|
||||
this.deprecatedTagsSyntax = false
|
||||
} else {
|
||||
this.tags = []
|
||||
this.deprecatedTagsSyntax = false
|
||||
}
|
||||
this.opengraph = rawData?.opengraph
|
||||
? new Map<string, string>(Object.entries(rawData.opengraph))
|
||||
: new Map<string, string>()
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new frontmatter metadata instance based on a raw yaml string.
|
||||
* @param rawYaml The frontmatter content in yaml format.
|
||||
* @throws Error when the content string is invalid yaml.
|
||||
* @return Frontmatter metadata instance containing the parsed properties from the yaml content.
|
||||
*/
|
||||
static createFromYaml(rawYaml: string): NoteFrontmatter {
|
||||
const rawNoteFrontmatter = load(rawYaml) as RawNoteFrontmatter
|
||||
return new NoteFrontmatter(rawNoteFrontmatter)
|
||||
}
|
||||
}
|
253
src/components/common/note-frontmatter/types.ts
Normal file
253
src/components/common/note-frontmatter/types.ts
Normal file
|
@ -0,0 +1,253 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
export type FrontmatterExtractionResult = PresentFrontmatterExtractionResult | NonPresentFrontmatterExtractionResult
|
||||
|
||||
export interface RendererFrontmatterInfo {
|
||||
offsetLines: number
|
||||
frontmatterInvalid: boolean
|
||||
deprecatedSyntax: boolean
|
||||
}
|
||||
|
||||
export interface PresentFrontmatterExtractionResult {
|
||||
frontmatterPresent: true
|
||||
rawFrontmatterText: string
|
||||
frontmatterLines: number
|
||||
}
|
||||
|
||||
interface NonPresentFrontmatterExtractionResult {
|
||||
frontmatterPresent: false
|
||||
}
|
||||
|
||||
export interface RawNoteFrontmatter {
|
||||
title: string | undefined
|
||||
description: string | undefined
|
||||
tags: string | string[] | undefined
|
||||
robots: string | undefined
|
||||
lang: string | undefined
|
||||
dir: string | undefined
|
||||
breaks: boolean | undefined
|
||||
GA: string | undefined
|
||||
disqus: string | undefined
|
||||
type: string | undefined
|
||||
slideOptions: unknown
|
||||
opengraph: { [key: string]: string } | null
|
||||
}
|
||||
|
||||
export const ISO6391 = [
|
||||
'aa',
|
||||
'ab',
|
||||
'af',
|
||||
'am',
|
||||
'ar',
|
||||
'ar-ae',
|
||||
'ar-bh',
|
||||
'ar-dz',
|
||||
'ar-eg',
|
||||
'ar-iq',
|
||||
'ar-jo',
|
||||
'ar-kw',
|
||||
'ar-lb',
|
||||
'ar-ly',
|
||||
'ar-ma',
|
||||
'ar-om',
|
||||
'ar-qa',
|
||||
'ar-sa',
|
||||
'ar-sy',
|
||||
'ar-tn',
|
||||
'ar-ye',
|
||||
'as',
|
||||
'ay',
|
||||
'de-at',
|
||||
'de-ch',
|
||||
'de-li',
|
||||
'de-lu',
|
||||
'div',
|
||||
'dz',
|
||||
'el',
|
||||
'en',
|
||||
'en-au',
|
||||
'en-bz',
|
||||
'en-ca',
|
||||
'en-gb',
|
||||
'en-ie',
|
||||
'en-jm',
|
||||
'en-nz',
|
||||
'en-ph',
|
||||
'en-tt',
|
||||
'en-us',
|
||||
'en-za',
|
||||
'en-zw',
|
||||
'eo',
|
||||
'es',
|
||||
'es-ar',
|
||||
'es-bo',
|
||||
'es-cl',
|
||||
'es-co',
|
||||
'es-cr',
|
||||
'es-do',
|
||||
'es-ec',
|
||||
'es-es',
|
||||
'es-gt',
|
||||
'es-hn',
|
||||
'es-mx',
|
||||
'es-ni',
|
||||
'es-pa',
|
||||
'es-pe',
|
||||
'es-pr',
|
||||
'es-py',
|
||||
'es-sv',
|
||||
'es-us',
|
||||
'es-uy',
|
||||
'es-ve',
|
||||
'et',
|
||||
'eu',
|
||||
'fa',
|
||||
'fi',
|
||||
'fj',
|
||||
'fo',
|
||||
'fr',
|
||||
'fr-be',
|
||||
'fr-ca',
|
||||
'fr-ch',
|
||||
'fr-lu',
|
||||
'fr-mc',
|
||||
'fy',
|
||||
'ga',
|
||||
'gd',
|
||||
'gl',
|
||||
'gn',
|
||||
'gu',
|
||||
'ha',
|
||||
'he',
|
||||
'hi',
|
||||
'hr',
|
||||
'hu',
|
||||
'hy',
|
||||
'ia',
|
||||
'id',
|
||||
'ie',
|
||||
'ik',
|
||||
'in',
|
||||
'is',
|
||||
'it',
|
||||
'it-ch',
|
||||
'iw',
|
||||
'ja',
|
||||
'ji',
|
||||
'jw',
|
||||
'ka',
|
||||
'kk',
|
||||
'kl',
|
||||
'km',
|
||||
'kn',
|
||||
'ko',
|
||||
'kok',
|
||||
'ks',
|
||||
'ku',
|
||||
'ky',
|
||||
'kz',
|
||||
'la',
|
||||
'ln',
|
||||
'lo',
|
||||
'ls',
|
||||
'lt',
|
||||
'lv',
|
||||
'mg',
|
||||
'mi',
|
||||
'mk',
|
||||
'ml',
|
||||
'mn',
|
||||
'mo',
|
||||
'mr',
|
||||
'ms',
|
||||
'mt',
|
||||
'my',
|
||||
'na',
|
||||
'nb-no',
|
||||
'ne',
|
||||
'nl',
|
||||
'nl-be',
|
||||
'nn-no',
|
||||
'no',
|
||||
'oc',
|
||||
'om',
|
||||
'or',
|
||||
'pa',
|
||||
'pl',
|
||||
'ps',
|
||||
'pt',
|
||||
'pt-br',
|
||||
'qu',
|
||||
'rm',
|
||||
'rn',
|
||||
'ro',
|
||||
'ro-md',
|
||||
'ru',
|
||||
'ru-md',
|
||||
'rw',
|
||||
'sa',
|
||||
'sb',
|
||||
'sd',
|
||||
'sg',
|
||||
'sh',
|
||||
'si',
|
||||
'sk',
|
||||
'sl',
|
||||
'sm',
|
||||
'sn',
|
||||
'so',
|
||||
'sq',
|
||||
'sr',
|
||||
'ss',
|
||||
'st',
|
||||
'su',
|
||||
'sv',
|
||||
'sv-fi',
|
||||
'sw',
|
||||
'sx',
|
||||
'syr',
|
||||
'ta',
|
||||
'te',
|
||||
'tg',
|
||||
'th',
|
||||
'ti',
|
||||
'tk',
|
||||
'tl',
|
||||
'tn',
|
||||
'to',
|
||||
'tr',
|
||||
'ts',
|
||||
'tt',
|
||||
'tw',
|
||||
'uk',
|
||||
'ur',
|
||||
'us',
|
||||
'uz',
|
||||
'vi',
|
||||
'vo',
|
||||
'wo',
|
||||
'xh',
|
||||
'yi',
|
||||
'yo',
|
||||
'zh',
|
||||
'zh-cn',
|
||||
'zh-hk',
|
||||
'zh-mo',
|
||||
'zh-sg',
|
||||
'zh-tw',
|
||||
'zu'
|
||||
] as const
|
||||
|
||||
export enum NoteType {
|
||||
DOCUMENT = '',
|
||||
SLIDE = 'slide'
|
||||
}
|
||||
|
||||
export enum NoteTextDirection {
|
||||
LTR = 'ltr',
|
||||
RTL = 'rtl'
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue