2022-03-31 12:28:07 +00:00
|
|
|
import { Prec } from "@codemirror/state";
|
|
|
|
import { KeyBinding, keymap } from "@codemirror/view";
|
|
|
|
import {
|
|
|
|
Language,
|
|
|
|
LanguageDescription,
|
|
|
|
LanguageSupport,
|
|
|
|
} from "@codemirror/language";
|
|
|
|
import { MarkdownExtension, MarkdownParser, parseCode } from "@lezer/markdown";
|
|
|
|
import { html } from "@codemirror/lang-html";
|
|
|
|
import {
|
|
|
|
commonmarkLanguage,
|
|
|
|
getCodeParser,
|
|
|
|
markdownLanguage,
|
|
|
|
mkLang,
|
|
|
|
} from "./markdown";
|
|
|
|
import { deleteMarkupBackward, insertNewlineContinueMarkup } from "./commands";
|
|
|
|
|
|
|
|
export {
|
|
|
|
commonmarkLanguage,
|
|
|
|
markdownLanguage,
|
|
|
|
insertNewlineContinueMarkup,
|
|
|
|
deleteMarkupBackward,
|
|
|
|
};
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
/// A small keymap with Markdown-specific bindings. Binds Enter to
|
|
|
|
/// [`insertNewlineContinueMarkup`](#lang-markdown.insertNewlineContinueMarkup)
|
|
|
|
/// and Backspace to
|
|
|
|
/// [`deleteMarkupBackward`](#lang-markdown.deleteMarkupBackward).
|
|
|
|
export const markdownKeymap: readonly KeyBinding[] = [
|
2022-03-31 12:28:07 +00:00
|
|
|
{ key: "Enter", run: insertNewlineContinueMarkup },
|
|
|
|
{ key: "Backspace", run: deleteMarkupBackward },
|
|
|
|
];
|
2022-03-20 08:56:28 +00:00
|
|
|
|
2022-03-31 12:28:07 +00:00
|
|
|
const htmlNoMatch = html({ matchClosingTags: false });
|
2022-03-20 08:56:28 +00:00
|
|
|
|
|
|
|
/// Markdown language support.
|
2022-03-31 12:28:07 +00:00
|
|
|
export function markdown(
|
2022-03-31 15:25:34 +00:00
|
|
|
config: {
|
|
|
|
/// When given, this language will be used by default to parse code
|
|
|
|
/// blocks.
|
|
|
|
defaultCodeLanguage?: Language | LanguageSupport;
|
|
|
|
/// A collection of language descriptions to search through for a
|
|
|
|
/// matching language (with
|
|
|
|
/// [`LanguageDescription.matchLanguageName`](#language.LanguageDescription^matchLanguageName))
|
|
|
|
/// when a fenced code block has an info string.
|
|
|
|
codeLanguages?: readonly LanguageDescription[];
|
|
|
|
/// Set this to false to disable installation of the Markdown
|
|
|
|
/// [keymap](#lang-markdown.markdownKeymap).
|
|
|
|
addKeymap?: boolean;
|
|
|
|
/// Markdown parser
|
|
|
|
/// [extensions](https://github.com/lezer-parser/markdown#user-content-markdownextension)
|
|
|
|
/// to add to the parser.
|
|
|
|
extensions?: MarkdownExtension;
|
|
|
|
/// The base language to use. Defaults to
|
|
|
|
/// [`commonmarkLanguage`](#lang-markdown.commonmarkLanguage).
|
|
|
|
base?: Language;
|
|
|
|
} = {}
|
2022-03-31 12:28:07 +00:00
|
|
|
) {
|
|
|
|
let {
|
|
|
|
codeLanguages,
|
|
|
|
defaultCodeLanguage,
|
|
|
|
addKeymap = true,
|
2022-03-31 15:25:34 +00:00
|
|
|
base: { parser } = commonmarkLanguage,
|
2022-03-31 12:28:07 +00:00
|
|
|
} = config;
|
|
|
|
if (!(parser instanceof MarkdownParser))
|
|
|
|
throw new RangeError(
|
2022-03-31 15:25:34 +00:00
|
|
|
"Base parser provided to `markdown` should be a Markdown parser"
|
2022-03-31 12:28:07 +00:00
|
|
|
);
|
|
|
|
let extensions = config.extensions ? [config.extensions] : [];
|
|
|
|
let support = [htmlNoMatch.support],
|
2022-03-31 15:25:34 +00:00
|
|
|
defaultCode;
|
2022-03-20 08:56:28 +00:00
|
|
|
if (defaultCodeLanguage instanceof LanguageSupport) {
|
2022-03-31 12:28:07 +00:00
|
|
|
support.push(defaultCodeLanguage.support);
|
|
|
|
defaultCode = defaultCodeLanguage.language;
|
2022-03-20 08:56:28 +00:00
|
|
|
} else if (defaultCodeLanguage) {
|
2022-03-31 12:28:07 +00:00
|
|
|
defaultCode = defaultCodeLanguage;
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|
2022-03-31 12:28:07 +00:00
|
|
|
let codeParser =
|
2022-03-31 15:25:34 +00:00
|
|
|
codeLanguages || defaultCode
|
|
|
|
? getCodeParser(codeLanguages || [], defaultCode)
|
|
|
|
: undefined;
|
2022-03-31 12:28:07 +00:00
|
|
|
extensions.push(
|
2022-03-31 15:25:34 +00:00
|
|
|
parseCode({ codeParser, htmlParser: htmlNoMatch.language.parser })
|
2022-03-31 12:28:07 +00:00
|
|
|
);
|
|
|
|
if (addKeymap) support.push(Prec.high(keymap.of(markdownKeymap)));
|
|
|
|
return new LanguageSupport(mkLang(parser.configure(extensions)), support);
|
2022-03-20 08:56:28 +00:00
|
|
|
}
|