-
-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·155 lines (136 loc) · 3.91 KB
/
index.js
File metadata and controls
executable file
·155 lines (136 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env node
/**
* @import {VirtualCodePlugin} from '@mdx-js/language-service'
* @import {PluggableList} from 'unified'
*/
import assert from 'node:assert'
import {createRequire} from 'node:module'
import path from 'node:path'
import process from 'node:process'
import {
createMdxLanguagePlugin,
createMdxServicePlugin,
resolvePlugins
} from '@mdx-js/language-service'
import {
createConnection,
createServer,
createTypeScriptProject,
loadTsdkByPath
} from '@volar/language-server/node.js'
import remarkFrontmatter from 'remark-frontmatter'
import remarkGfm from 'remark-gfm'
import {create as createMarkdownServicePlugin} from 'volar-service-markdown'
import {create as createTypeScriptServicePlugin} from 'volar-service-typescript'
import {create as createTypeScriptSyntacticServicePlugin} from 'volar-service-typescript/lib/plugins/syntactic.js'
process.title = 'mdx-language-server'
/** @type {PluggableList} */
const defaultPlugins = [[remarkFrontmatter, ['toml', 'yaml']], remarkGfm]
const connection = createConnection()
const server = createServer(connection)
let tsEnabled = false
connection.onInitialize(async (parameters) => {
const tsdk = parameters.initializationOptions?.typescript?.tsdk
tsEnabled = Boolean(parameters.initializationOptions?.typescript?.enabled)
assert.ok(
typeof tsdk === 'string',
'Missing initialization option typescript.tsdk'
)
const {typescript, diagnosticMessages} = loadTsdkByPath(
tsdk,
parameters.locale
)
return server.initialize(
parameters,
createTypeScriptProject(
typescript,
diagnosticMessages,
({configFileName}) => {
// Workaround for https://github.com/volarjs/volar.js/issues/283
configFileName &&= typescript.findConfigFile(
configFileName,
typescript.sys.fileExists
)
return {
languagePlugins: getLanguagePlugins(configFileName)
}
}
),
getLanguageServicePlugins()
)
function getLanguageServicePlugins() {
const plugins = [
createMarkdownServicePlugin({
getDiagnosticOptions(document, context) {
return context.env.getConfiguration?.('mdx.validate')
}
}),
createMdxServicePlugin(connection.workspace)
]
if (tsEnabled) {
plugins.push(...createTypeScriptServicePlugin(typescript, {}))
} else {
plugins.push(createTypeScriptSyntacticServicePlugin(typescript))
}
return plugins
}
/**
* @param {string | undefined} tsconfig
*/
function getLanguagePlugins(tsconfig) {
/** @type {PluggableList | undefined} */
let remarkPlugins
/** @type {VirtualCodePlugin[] | undefined} */
let virtualCodePlugins
let checkMdx = false
let jsxImportSource = 'react'
if (tsconfig) {
const cwd = path.dirname(tsconfig)
const configSourceFile = typescript.readJsonConfigFile(
tsconfig,
typescript.sys.readFile
)
const commandLine = typescript.parseJsonSourceFileConfigFileContent(
configSourceFile,
typescript.sys,
cwd,
undefined,
tsconfig
)
const require = createRequire(tsconfig)
;[remarkPlugins, virtualCodePlugins] = resolvePlugins(
commandLine.raw?.mdx,
(name) => require(name).default
)
checkMdx = Boolean(commandLine.raw?.mdx?.checkMdx)
jsxImportSource = commandLine.options.jsxImportSource || jsxImportSource
}
return [
createMdxLanguagePlugin(
remarkPlugins || defaultPlugins,
virtualCodePlugins,
checkMdx,
jsxImportSource
)
]
}
})
connection.onInitialized(() => {
const extensions = ['mdx']
if (tsEnabled) {
extensions.push(
'cjs',
'cts',
'js',
'jsx',
'json',
'mjs',
'mts',
'ts',
'tsx'
)
}
server.initialized()
server.fileWatcher.watchFiles([`**/*.{${extensions.join(',')}}`])
})
connection.listen()