support creating a Language with just a Parser #7

Closed
cdmistman wants to merge 1 commit from patch-1 into main
cdmistman commented 2024-01-14 01:24:55 +01:00 (Migrated from github.com)

i couldn't find a way to create a new LanguageSupport or Language by extending the commonmark/GFM flavored markdowns already defined in this package. After reading through the source, it seems it was most intended to use the markdown function to do this. however, the type constraints on the argument to that function make this infeasible, having to write something like:

import { markdown, markdownLanguage } from '@codemirror/lang-markdown';
import type { LanguageSupport } from '@codemirror/language';
import { parser as markdownParser } from '@lezer/markdown';

markdownLanguage.parser = markdownParser.configure({})

const markdownLanguageSupport: LanguageSupport = markdown({
	base: markdownLanguage,
});

this change is API-compatible and allows accomplishing what i'm trying to accomplish like the following. note that only the parser field of the base arg is actually used within the markdown function

import { markdown } from '@codemirror/lang-markdown';
import type { LanguageSupport } from '@codemirror/language';
import { parser as markdownParser } from '@lezer/markdown';

const markdownLanguageSupport: LanguageSupport = markdown({
	base: { parser: markdownParser.configure({}) },
});
i couldn't find a way to create a new `LanguageSupport` or `Language` by extending the commonmark/GFM flavored markdowns already defined in this package. After reading through the source, it seems it was most intended to use the `markdown` function to do this. however, the type constraints on the argument to that function make this infeasible, having to write something like: ```ts import { markdown, markdownLanguage } from '@codemirror/lang-markdown'; import type { LanguageSupport } from '@codemirror/language'; import { parser as markdownParser } from '@lezer/markdown'; markdownLanguage.parser = markdownParser.configure({}) const markdownLanguageSupport: LanguageSupport = markdown({ base: markdownLanguage, }); ``` this change is API-compatible and allows accomplishing what i'm trying to accomplish like the following. note that only the `parser` field of the `base` arg is actually used within the `markdown` function ```ts import { markdown } from '@codemirror/lang-markdown'; import type { LanguageSupport } from '@codemirror/language'; import { parser as markdownParser } from '@lezer/markdown'; const markdownLanguageSupport: LanguageSupport = markdown({ base: { parser: markdownParser.configure({}) }, }); ```
cdmistman commented 2024-01-14 01:33:13 +01:00 (Migrated from github.com)

for context, the motivation for attempting to do this is adding tags to the parser output :)

for context, the motivation for attempting to do this is adding `tags` to the parser output :)
marijnh commented 2024-01-14 15:39:47 +01:00 (Migrated from github.com)

Doesn't passing your configuration directly via extensions make this a lot easier?

Doesn't passing your configuration directly via [`extensions`](https://github.com/codemirror/lang-markdown#user-content-markdown^config.extensions) make this a lot easier?
cdmistman commented 2024-01-15 18:11:58 +01:00 (Migrated from github.com)

ah, yes you're right 🤦 thank you!

ah, yes you're right 🤦 thank you!

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
codemirror/lang-markdown!7
No description provided.