hedgedoc/src/components/editor-page/editor-pane/linter/linter.ts
Tilman Vatteroth 4e18ce38f3 feat: add settings dialog
Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
2022-11-15 22:09:02 +01:00

43 lines
1.5 KiB
TypeScript

/*
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import type { Diagnostic } from '@codemirror/lint'
import { linter } from '@codemirror/lint'
import { useMemo } from 'react'
import type { Extension } from '@codemirror/state'
import type { EditorView } from '@codemirror/view'
import { optionalAppExtensions } from '../../../../extensions/extra-integrations/optional-app-extensions'
import { FrontmatterLinter } from './frontmatter-linter'
import { useDarkModeState } from '../../../../hooks/common/use-dark-mode-state'
/**
* The Linter interface.
*
* This should be implemented by each linter we want to use.
*/
export interface Linter {
lint(view: EditorView): Diagnostic[]
}
const createLinterExtension = () =>
linter((view) =>
optionalAppExtensions
.flatMap((extension) => extension.buildCodeMirrorLinter())
.concat(new FrontmatterLinter())
.flatMap((aLinter) => aLinter.lint(view))
)
/**
* Creates a codemirror linter that runs all markdown extension linters.
* Due to a bug in codemirror that breaks the "fix" buttons when switching themes, the extension is recreated if the app switches between dark and light mode.
*
* @return The build codemirror linter extension
*/
export const useLinter = (): Extension => {
const darkModeActivated = useDarkModeState()
return useMemo(() => (darkModeActivated ? createLinterExtension() : createLinterExtension()), [darkModeActivated])
}