Skip to content

Commit 0d5c7eb

Browse files
authored
fix(language-core): do not set template lang to md for markdown (#5497)
1 parent 109e9d1 commit 0d5c7eb

File tree

1 file changed

+28
-34
lines changed

1 file changed

+28
-34
lines changed

packages/language-core/lib/plugins/file-md.ts

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type CodeInformation, defaultMapperFactory, type Mapping } from '@volar/language-core';
1+
import { SourceMap } from '@volar/language-core';
22
import type { SFCBlock } from '@vue/compiler-sfc';
33
import { type Segment, toString } from 'muggle-string';
44
import type { VueLanguagePlugin } from '../types';
@@ -10,9 +10,9 @@ const codeblockReg = /(`{3,})[\s\S]+?\1/g;
1010
const inlineCodeblockReg = /`[^\n`]+?`/g;
1111
const latexBlockReg = /(\${2,})[\s\S]+?\1/g;
1212
const scriptSetupReg = /\\<[\s\S]+?>\n?/g;
13-
const sfcBlockReg = /<(script|style)\b[\s\S]*?>([\s\S]*?)<\/\1>/g;
1413
const angleBracketReg = /<\S*:\S*>/g;
1514
const linkReg = /\[[\s\S]*?\]\([\s\S]*?\)/g;
15+
const sfcBlockReg = /<(script|style)\b[\s\S]*?>([\s\S]*?)<\/\1>/g;
1616
const codeSnippetImportReg = /^\s*<<<\s*.+/gm;
1717

1818
const plugin: VueLanguagePlugin = ({ vueCompilerOptions }) => {
@@ -46,48 +46,42 @@ const plugin: VueLanguagePlugin = ({ vueCompilerOptions }) => {
4646
// # \<script setup>
4747
.replace(scriptSetupReg, match => ' '.repeat(match.length))
4848
// <<< https://vitepress.dev/guide/markdown#import-code-snippets
49-
.replace(codeSnippetImportReg, match => ' '.repeat(match.length));
49+
.replace(codeSnippetImportReg, match => ' '.repeat(match.length))
50+
// angle bracket: <http://foo.com>
51+
.replace(angleBracketReg, match => ' '.repeat(match.length))
52+
// [foo](http://foo.com)
53+
.replace(linkReg, match => ' '.repeat(match.length));
5054

5155
const codes: Segment[] = [];
5256

5357
for (const match of content.matchAll(sfcBlockReg)) {
54-
if (match.index !== undefined) {
55-
const matchText = match[0];
56-
codes.push([matchText, undefined, match.index]);
57-
codes.push('\n\n');
58-
content = content.slice(0, match.index) + ' '.repeat(matchText.length)
59-
+ content.slice(match.index + matchText.length);
60-
}
58+
const matchText = match[0];
59+
codes.push([matchText, undefined, match.index]);
60+
codes.push('\n\n');
61+
content = content.slice(0, match.index) + ' '.repeat(matchText.length)
62+
+ content.slice(match.index + matchText.length);
6163
}
6264

63-
content = content
64-
// angle bracket: <http://foo.com>
65-
.replace(angleBracketReg, match => ' '.repeat(match.length))
66-
// [foo](http://foo.com)
67-
.replace(linkReg, match => ' '.repeat(match.length));
68-
6965
codes.push('<template>\n');
7066
codes.push([content, undefined, 0]);
7167
codes.push('\n</template>');
7268

73-
const file2VueSourceMap = defaultMapperFactory(buildMappings(codes) as unknown as Mapping<CodeInformation>[]);
69+
const mappings = buildMappings(codes);
70+
const mapper = new SourceMap(mappings);
7471
const sfc = parse(toString(codes));
7572

76-
if (sfc.descriptor.template) {
77-
sfc.descriptor.template.lang = 'md';
78-
transformRange(sfc.descriptor.template);
79-
}
80-
if (sfc.descriptor.script) {
81-
transformRange(sfc.descriptor.script);
82-
}
83-
if (sfc.descriptor.scriptSetup) {
84-
transformRange(sfc.descriptor.scriptSetup);
85-
}
86-
for (const style of sfc.descriptor.styles) {
87-
transformRange(style);
88-
}
89-
for (const customBlock of sfc.descriptor.customBlocks) {
90-
transformRange(customBlock);
73+
for (
74+
const block of [
75+
sfc.descriptor.template,
76+
sfc.descriptor.script,
77+
sfc.descriptor.scriptSetup,
78+
...sfc.descriptor.styles,
79+
...sfc.descriptor.customBlocks,
80+
]
81+
) {
82+
if (block) {
83+
transformRange(block);
84+
}
9185
}
9286

9387
return sfc;
@@ -98,11 +92,11 @@ const plugin: VueLanguagePlugin = ({ vueCompilerOptions }) => {
9892
const endOffset = end.offset;
9993
start.offset = -1;
10094
end.offset = -1;
101-
for (const [offset] of file2VueSourceMap.toSourceLocation(startOffset)) {
95+
for (const [offset] of mapper.toSourceLocation(startOffset)) {
10296
start.offset = offset;
10397
break;
10498
}
105-
for (const [offset] of file2VueSourceMap.toSourceLocation(endOffset)) {
99+
for (const [offset] of mapper.toSourceLocation(endOffset)) {
106100
end.offset = offset;
107101
break;
108102
}

0 commit comments

Comments
 (0)