@astrojs/ts-plugin 1.9.3 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/astro2tsx.js +1 -2
- package/dist/frontmatter.d.ts +24 -0
- package/dist/frontmatter.js +90 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +26 -3
- package/dist/language.js +2 -2
- package/package.json +7 -4
package/dist/astro2tsx.js
CHANGED
|
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.astro2tsx =
|
|
6
|
+
exports.astro2tsx = astro2tsx;
|
|
7
7
|
const node_path_1 = __importDefault(require("node:path"));
|
|
8
8
|
const sync_1 = require("@astrojs/compiler/sync");
|
|
9
9
|
const sourcemap_codec_1 = require("@jridgewell/sourcemap-codec");
|
|
@@ -53,7 +53,6 @@ function astro2tsx(input, fileName) {
|
|
|
53
53
|
diagnostics: tsx.diagnostics,
|
|
54
54
|
};
|
|
55
55
|
}
|
|
56
|
-
exports.astro2tsx = astro2tsx;
|
|
57
56
|
function getVirtualFileTSX(input, tsx, fileName) {
|
|
58
57
|
tsx.code = patchTSX(tsx.code, fileName);
|
|
59
58
|
const v3Mappings = (0, sourcemap_codec_1.decode)(tsx.map.mappings);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type CodeMapping, type LanguagePlugin, type VirtualCode } from '@volar/language-core';
|
|
2
|
+
import type ts from 'typescript';
|
|
3
|
+
export declare const frontmatterRE: RegExp;
|
|
4
|
+
export type CollectionConfig = {
|
|
5
|
+
folder: string;
|
|
6
|
+
config: {
|
|
7
|
+
collections: {
|
|
8
|
+
hasSchema: boolean;
|
|
9
|
+
name: string;
|
|
10
|
+
}[];
|
|
11
|
+
entries: Record<string, string>;
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
export declare function getFrontmatterLanguagePlugin(collectionConfigs: CollectionConfig[]): LanguagePlugin<string, FrontmatterHolder>;
|
|
15
|
+
export declare class FrontmatterHolder implements VirtualCode {
|
|
16
|
+
fileName: string;
|
|
17
|
+
languageId: string;
|
|
18
|
+
snapshot: ts.IScriptSnapshot;
|
|
19
|
+
collection: string | undefined;
|
|
20
|
+
id: string;
|
|
21
|
+
mappings: CodeMapping[];
|
|
22
|
+
embeddedCodes: VirtualCode[];
|
|
23
|
+
constructor(fileName: string, languageId: string, snapshot: ts.IScriptSnapshot, collection: string | undefined);
|
|
24
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FrontmatterHolder = exports.frontmatterRE = void 0;
|
|
4
|
+
exports.getFrontmatterLanguagePlugin = getFrontmatterLanguagePlugin;
|
|
5
|
+
const node_url_1 = require("node:url");
|
|
6
|
+
const yaml2ts_1 = require("@astrojs/yaml2ts");
|
|
7
|
+
const language_core_1 = require("@volar/language-core");
|
|
8
|
+
const SUPPORTED_FRONTMATTER_EXTENSIONS = { md: 'markdown', mdx: 'mdx', mdoc: 'mdoc' };
|
|
9
|
+
const SUPPORTED_FRONTMATTER_EXTENSIONS_KEYS = Object.keys(SUPPORTED_FRONTMATTER_EXTENSIONS);
|
|
10
|
+
const SUPPORTED_FRONTMATTER_EXTENSIONS_VALUES = Object.values(SUPPORTED_FRONTMATTER_EXTENSIONS);
|
|
11
|
+
exports.frontmatterRE = /^---(.*?)^---/ms;
|
|
12
|
+
function getCollectionName(collectionConfigs, fsPath) {
|
|
13
|
+
for (const collection of collectionConfigs) {
|
|
14
|
+
if (collection.config.entries[fsPath]) {
|
|
15
|
+
return collection.config.entries[fsPath];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function getFrontmatterLanguagePlugin(collectionConfigs) {
|
|
20
|
+
return {
|
|
21
|
+
getLanguageId(scriptId) {
|
|
22
|
+
const fileType = SUPPORTED_FRONTMATTER_EXTENSIONS_KEYS.find((ext) => scriptId.endsWith(`.${ext}`));
|
|
23
|
+
if (fileType) {
|
|
24
|
+
return SUPPORTED_FRONTMATTER_EXTENSIONS[fileType];
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
createVirtualCode(scriptId, languageId, snapshot) {
|
|
28
|
+
if (SUPPORTED_FRONTMATTER_EXTENSIONS_VALUES.includes(languageId)) {
|
|
29
|
+
const fileName = scriptId.replace(/\\/g, '/');
|
|
30
|
+
return new FrontmatterHolder(fileName, languageId, snapshot,
|
|
31
|
+
// In TypeScript plugins, unlike in the language server, the scriptId is just a string file path
|
|
32
|
+
// so we'll have to convert it to a URL to match the collection config entries
|
|
33
|
+
getCollectionName(collectionConfigs, (0, node_url_1.pathToFileURL)(fileName).toString().toLowerCase()));
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
typescript: {
|
|
37
|
+
extraFileExtensions: SUPPORTED_FRONTMATTER_EXTENSIONS_KEYS.map((ext) => ({
|
|
38
|
+
extension: ext,
|
|
39
|
+
isMixedContent: true,
|
|
40
|
+
scriptKind: 7,
|
|
41
|
+
})),
|
|
42
|
+
getServiceScript(astroCode) {
|
|
43
|
+
for (const code of (0, language_core_1.forEachEmbeddedCode)(astroCode)) {
|
|
44
|
+
if (code.id === yaml2ts_1.VIRTUAL_CODE_ID) {
|
|
45
|
+
return {
|
|
46
|
+
code,
|
|
47
|
+
extension: '.ts',
|
|
48
|
+
scriptKind: 3,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return undefined;
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
class FrontmatterHolder {
|
|
58
|
+
constructor(fileName, languageId, snapshot, collection) {
|
|
59
|
+
this.fileName = fileName;
|
|
60
|
+
this.languageId = languageId;
|
|
61
|
+
this.snapshot = snapshot;
|
|
62
|
+
this.collection = collection;
|
|
63
|
+
this.id = 'frontmatter-holder';
|
|
64
|
+
this.mappings = [
|
|
65
|
+
{
|
|
66
|
+
sourceOffsets: [0],
|
|
67
|
+
generatedOffsets: [0],
|
|
68
|
+
lengths: [this.snapshot.getLength()],
|
|
69
|
+
data: {
|
|
70
|
+
verification: true,
|
|
71
|
+
completion: true,
|
|
72
|
+
semantic: true,
|
|
73
|
+
navigation: true,
|
|
74
|
+
structure: true,
|
|
75
|
+
format: true,
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
];
|
|
79
|
+
this.embeddedCodes = [];
|
|
80
|
+
this.snapshot = snapshot;
|
|
81
|
+
if (!this.collection)
|
|
82
|
+
return;
|
|
83
|
+
const frontmatterContent = exports.frontmatterRE.exec(this.snapshot.getText(0, this.snapshot.getLength()))?.[0];
|
|
84
|
+
if (!frontmatterContent)
|
|
85
|
+
return;
|
|
86
|
+
const yaml2tsResult = (0, yaml2ts_1.yaml2ts)(frontmatterContent, this.collection);
|
|
87
|
+
this.embeddedCodes.push(yaml2tsResult.virtualCode);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.FrontmatterHolder = FrontmatterHolder;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
declare const _default:
|
|
1
|
+
declare const _default: import("typescript").server.PluginModuleFactory;
|
|
2
2
|
export = _default;
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
const createLanguageServicePlugin_js_1 = require("@volar/typescript/lib/quickstart/createLanguageServicePlugin.js");
|
|
3
|
+
const frontmatter_js_1 = require("./frontmatter.js");
|
|
3
4
|
const language_js_1 = require("./language.js");
|
|
4
|
-
module.exports = (0, createLanguageServicePlugin_js_1.createLanguageServicePlugin)(() =>
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
module.exports = (0, createLanguageServicePlugin_js_1.createLanguageServicePlugin)((ts, info) => {
|
|
6
|
+
let collectionConfig = undefined;
|
|
7
|
+
try {
|
|
8
|
+
const currentDir = info.project.getCurrentDirectory();
|
|
9
|
+
const fileContent = ts.sys.readFile(currentDir + '/.astro/collections/collections.json');
|
|
10
|
+
if (fileContent) {
|
|
11
|
+
collectionConfig = {
|
|
12
|
+
folder: currentDir,
|
|
13
|
+
config: JSON.parse(fileContent),
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
catch (err) {
|
|
18
|
+
// If the file doesn't exist, we don't really care, but if it's something else, we want to know
|
|
19
|
+
if (err && err.code !== 'ENOENT')
|
|
20
|
+
console.error(err);
|
|
21
|
+
}
|
|
22
|
+
let languagePlugins = [(0, language_js_1.getLanguagePlugin)()];
|
|
23
|
+
if (collectionConfig) {
|
|
24
|
+
languagePlugins.push((0, frontmatter_js_1.getFrontmatterLanguagePlugin)([collectionConfig]));
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
languagePlugins,
|
|
28
|
+
};
|
|
29
|
+
});
|
package/dist/language.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/// <reference types="@volar/typescript" />
|
|
3
3
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.AstroVirtualCode =
|
|
4
|
+
exports.AstroVirtualCode = void 0;
|
|
5
|
+
exports.getLanguagePlugin = getLanguagePlugin;
|
|
5
6
|
const language_core_1 = require("@volar/language-core");
|
|
6
7
|
const astro2tsx_js_1 = require("./astro2tsx.js");
|
|
7
8
|
function getLanguagePlugin() {
|
|
@@ -32,7 +33,6 @@ function getLanguagePlugin() {
|
|
|
32
33
|
},
|
|
33
34
|
};
|
|
34
35
|
}
|
|
35
|
-
exports.getLanguagePlugin = getLanguagePlugin;
|
|
36
36
|
class AstroVirtualCode {
|
|
37
37
|
constructor(fileName, snapshot) {
|
|
38
38
|
this.fileName = fileName;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astrojs/ts-plugin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.10.0",
|
|
4
4
|
"description": "A TypeScript Plugin providing Astro intellisense",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "commonjs",
|
|
@@ -22,10 +22,11 @@
|
|
|
22
22
|
"author": "withastro",
|
|
23
23
|
"license": "MIT",
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@volar/language-core": "~2.4.0-alpha.15",
|
|
26
|
-
"@volar/typescript": "~2.4.0-alpha.15",
|
|
27
25
|
"@astrojs/compiler": "^2.10.3",
|
|
26
|
+
"@astrojs/yaml2ts": "^0.2.0",
|
|
28
27
|
"@jridgewell/sourcemap-codec": "^1.4.15",
|
|
28
|
+
"@volar/language-core": "~2.4.0-alpha.15",
|
|
29
|
+
"@volar/typescript": "~2.4.0-alpha.15",
|
|
29
30
|
"semver": "^7.3.8",
|
|
30
31
|
"vscode-languageserver-textdocument": "^1.0.11"
|
|
31
32
|
},
|
|
@@ -36,7 +37,9 @@
|
|
|
36
37
|
"@types/semver": "^7.3.13",
|
|
37
38
|
"chai": "^4.3.7",
|
|
38
39
|
"glob": "^8.0.3",
|
|
39
|
-
"mocha": "^10.2.0"
|
|
40
|
+
"mocha": "^10.2.0",
|
|
41
|
+
"vscode-uri": "^3.0.8",
|
|
42
|
+
"typescript": "^5.5.4"
|
|
40
43
|
},
|
|
41
44
|
"scripts": {
|
|
42
45
|
"build": "tsc",
|