Skip to content

Commit 31e2a79

Browse files
authored
feat(markup): added yfmLang autocomplete options (#315)
* feat(markup): added yfmLang autocomplete options
1 parent 35b927e commit 31e2a79

File tree

1 file changed

+65
-52
lines changed

1 file changed

+65
-52
lines changed

src/markup/codemirror/yfm.ts

Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,66 @@ export const yfmNoteSnippets: Record<YfmNoteType, ReturnType<typeof snippet>> =
7676
export const yfmCutSnippetTemplate = '{% cut "#{title}" %}\n\n#{}\n\n{% endcut %}\n\n';
7777
export const yfmCutSnippet = snippet(yfmCutSnippetTemplate);
7878

79-
export function yfmLang(): Extension {
79+
export interface LanguageData {
80+
autocomplete: CompletionSource;
81+
[key: string]: any;
82+
}
83+
84+
export interface YfmLangOptions {
85+
languageData?: LanguageData[];
86+
}
87+
88+
const mdAutocomplete: LanguageData = {
89+
autocomplete: (context) => {
90+
// TODO: add more actions and re-enable
91+
// let word = context.matchBefore(/\/.*/);
92+
// if (word) {
93+
// return {
94+
// from: word.from,
95+
// options: [
96+
// ...yfmNoteTypes.map<Completion>((type, index) => ({
97+
// label: `/yfm note ${type}`,
98+
// displayLabel: `YFM Note ${capitalize(type)}`,
99+
// type: 'text',
100+
// apply: yfmNoteSnippets[type],
101+
// boost: -index,
102+
// })),
103+
// {
104+
// label: '/yfm cut',
105+
// displayLabel: 'YFM Cut',
106+
// type: 'text',
107+
// apply: yfmCutSnippet,
108+
// },
109+
// ],
110+
// };
111+
// }
112+
const word = context.matchBefore(/^.*/);
113+
if (word?.text.startsWith('{%')) {
114+
return {
115+
from: word.from,
116+
options: [
117+
...yfmNoteTypes.map<Completion>((type, index) => ({
118+
label: `{% note ${type}`,
119+
displayLabel: capitalize(type),
120+
type: 'text',
121+
section: 'YFM Note',
122+
apply: yfmNoteSnippets[type],
123+
boost: -index,
124+
})),
125+
{
126+
label: '{% cut',
127+
displayLabel: 'YFM Cut',
128+
type: 'text',
129+
apply: yfmCutSnippet,
130+
},
131+
],
132+
};
133+
}
134+
return null;
135+
},
136+
};
137+
138+
export function yfmLang({languageData = []}: YfmLangOptions = {}): Extension {
80139
const mdSupport = markdown({
81140
// defaultCodeLanguage: markdownLanguage,
82141
base: markdownLanguage,
@@ -85,55 +144,9 @@ export function yfmLang(): Extension {
85144
extensions: [UnderlineExtension, MonospaceExtension, MarkedExtension],
86145
});
87146

88-
const mdAutocomplete: {autocomplete: CompletionSource} = {
89-
autocomplete: (context) => {
90-
// TODO: add more actions and re-enable
91-
// let word = context.matchBefore(/\/.*/);
92-
// if (word) {
93-
// return {
94-
// from: word.from,
95-
// options: [
96-
// ...yfmNoteTypes.map<Completion>((type, index) => ({
97-
// label: `/yfm note ${type}`,
98-
// displayLabel: `YFM Note ${capitalize(type)}`,
99-
// type: 'text',
100-
// apply: yfmNoteSnippets[type],
101-
// boost: -index,
102-
// })),
103-
// {
104-
// label: '/yfm cut',
105-
// displayLabel: 'YFM Cut',
106-
// type: 'text',
107-
// apply: yfmCutSnippet,
108-
// },
109-
// ],
110-
// };
111-
// }
112-
const word = context.matchBefore(/^.*/);
113-
if (word?.text.startsWith('{%')) {
114-
return {
115-
from: word.from,
116-
options: [
117-
...yfmNoteTypes.map<Completion>((type, index) => ({
118-
label: `{% note ${type}`,
119-
displayLabel: capitalize(type),
120-
type: 'text',
121-
section: 'YFM Note',
122-
apply: yfmNoteSnippets[type],
123-
boost: -index,
124-
})),
125-
{
126-
label: '{% cut',
127-
displayLabel: 'YFM Cut',
128-
type: 'text',
129-
apply: yfmCutSnippet,
130-
},
131-
],
132-
};
133-
}
134-
return null;
135-
},
136-
};
137-
138-
return [mdSupport, mdSupport.language.data.of(mdAutocomplete)];
147+
return [
148+
mdSupport,
149+
mdSupport.language.data.of(mdAutocomplete),
150+
languageData.map((item) => mdSupport.language.data.of(item)),
151+
];
139152
}

0 commit comments

Comments
 (0)