@flint.fyi/markdown-language 0.0.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/README.md +9 -0
- package/lib/createMarkdownFile.d.ts +8 -0
- package/lib/createMarkdownFile.js +33 -0
- package/lib/directives/parseDirectivesFromMarkdownFile.d.ts +6 -0
- package/lib/directives/parseDirectivesFromMarkdownFile.js +35 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +3 -0
- package/lib/language.d.ts +12 -0
- package/lib/language.js +29 -0
- package/lib/nodes.d.ts +30 -0
- package/lib/prepareMarkdownFile.d.ts +10 -0
- package/lib/prepareMarkdownFile.js +13 -0
- package/package.json +52 -0
package/README.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { FileDiskData, LanguageFileDefinition } from "@flint.fyi/core";
|
|
2
|
+
import type { MarkdownFileServices } from "./language.ts";
|
|
3
|
+
import type { MarkdownNodesByName } from "./nodes.ts";
|
|
4
|
+
export declare function createMarkdownFile(data: FileDiskData): {
|
|
5
|
+
languageFile: LanguageFileDefinition<MarkdownNodesByName, MarkdownFileServices>;
|
|
6
|
+
root: import("mdast").Root;
|
|
7
|
+
};
|
|
8
|
+
//# sourceMappingURL=createMarkdownFile.d.ts.map
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { fromMarkdown } from "mdast-util-from-markdown";
|
|
2
|
+
import { gfmFromMarkdown } from "mdast-util-gfm";
|
|
3
|
+
import { gfm } from "micromark-extension-gfm";
|
|
4
|
+
import { visit } from "unist-util-visit";
|
|
5
|
+
|
|
6
|
+
//#region src/createMarkdownFile.ts
|
|
7
|
+
function createMarkdownFile(data) {
|
|
8
|
+
const root = fromMarkdown(data.sourceText, {
|
|
9
|
+
extensions: [gfm()],
|
|
10
|
+
mdastExtensions: [gfmFromMarkdown()]
|
|
11
|
+
});
|
|
12
|
+
return {
|
|
13
|
+
languageFile: {
|
|
14
|
+
about: data,
|
|
15
|
+
runVisitors(options, runtime) {
|
|
16
|
+
if (!runtime.visitors) return;
|
|
17
|
+
const { visitors } = runtime;
|
|
18
|
+
const fileServices = {
|
|
19
|
+
options,
|
|
20
|
+
root
|
|
21
|
+
};
|
|
22
|
+
visit(root, (node) => {
|
|
23
|
+
visitors[node.type]?.(node, fileServices);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
root
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
//#endregion
|
|
32
|
+
export { createMarkdownFile };
|
|
33
|
+
//# sourceMappingURL=createMarkdownFile.js.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Root } from "mdast";
|
|
2
|
+
export declare function parseDirectivesFromMarkdownFile(root: Root, sourceText: string): {
|
|
3
|
+
directives: import("@flint.fyi/core").CommentDirective[];
|
|
4
|
+
reports: import("@flint.fyi/core").FileReport[];
|
|
5
|
+
};
|
|
6
|
+
//# sourceMappingURL=parseDirectivesFromMarkdownFile.d.ts.map
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { DirectivesCollector } from "@flint.fyi/core";
|
|
2
|
+
import { visit } from "unist-util-visit";
|
|
3
|
+
import { nullThrows } from "@flint.fyi/utils";
|
|
4
|
+
|
|
5
|
+
//#region src/directives/parseDirectivesFromMarkdownFile.ts
|
|
6
|
+
function parseDirectivesFromMarkdownFile(root, sourceText) {
|
|
7
|
+
const collector = new DirectivesCollector(root.children.find((child) => child.position?.start.offset !== void 0)?.position?.start.offset ?? sourceText.length);
|
|
8
|
+
visit(root, "html", (node) => {
|
|
9
|
+
const regex = /<!--([\s\S]*?)-->/g;
|
|
10
|
+
let commentMatch;
|
|
11
|
+
while ((commentMatch = regex.exec(node.value)) !== null) {
|
|
12
|
+
const directiveMatch = /^\s*flint-(\S+)(?:\s+(.+))?/.exec(nullThrows(commentMatch[1], "Directive match is expected to be present by the regex match"));
|
|
13
|
+
if (!directiveMatch) return;
|
|
14
|
+
const position = nullThrows(node.position, "Node position is expected to be present");
|
|
15
|
+
const [type, selection] = directiveMatch.slice(1);
|
|
16
|
+
collector.add({
|
|
17
|
+
begin: {
|
|
18
|
+
column: position.start.column - 1,
|
|
19
|
+
line: position.start.line - 1,
|
|
20
|
+
raw: nullThrows(position.start.offset, "Node start offset is expected to be present")
|
|
21
|
+
},
|
|
22
|
+
end: {
|
|
23
|
+
column: position.end.column - 1,
|
|
24
|
+
line: position.end.line - 1,
|
|
25
|
+
raw: nullThrows(position.end.offset, "Node end offset is expected to be present")
|
|
26
|
+
}
|
|
27
|
+
}, nullThrows(selection, "Selection is expected to be present by the regex match"), nullThrows(type, "Type is expected to be present by the regex match"));
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
return collector.collect();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
//#endregion
|
|
34
|
+
export { parseDirectivesFromMarkdownFile };
|
|
35
|
+
//# sourceMappingURL=parseDirectivesFromMarkdownFile.js.map
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { MarkdownNodesByName, PointWithOffset, PositionWithOffsets, RootContentMapWithChildren, WithPosition } from "./nodes.js";
|
|
2
|
+
import { MarkdownFileServices, markdownLanguage } from "./language.js";
|
|
3
|
+
export { MarkdownFileServices, MarkdownNodesByName, PointWithOffset, PositionWithOffsets, RootContentMapWithChildren, WithPosition, markdownLanguage };
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { MarkdownNodesByName, WithPosition } from "./nodes.js";
|
|
2
|
+
import * as _flint_fyi_core0 from "@flint.fyi/core";
|
|
3
|
+
import * as mdast from "mdast";
|
|
4
|
+
|
|
5
|
+
//#region src/language.d.ts
|
|
6
|
+
interface MarkdownFileServices {
|
|
7
|
+
root: WithPosition<mdast.Root>;
|
|
8
|
+
}
|
|
9
|
+
declare const markdownLanguage: _flint_fyi_core0.Language<MarkdownNodesByName, MarkdownFileServices>;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { MarkdownFileServices, markdownLanguage };
|
|
12
|
+
//# sourceMappingURL=language.d.ts.map
|
package/lib/language.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { createMarkdownFile } from "./createMarkdownFile.js";
|
|
2
|
+
import { prepareMarkdownFile } from "./prepareMarkdownFile.js";
|
|
3
|
+
import { createLanguage } from "@flint.fyi/core";
|
|
4
|
+
import fsSync from "node:fs";
|
|
5
|
+
|
|
6
|
+
//#region src/language.ts
|
|
7
|
+
const markdownLanguage = createLanguage({
|
|
8
|
+
about: { name: "Markdown" },
|
|
9
|
+
createFileFactory: () => {
|
|
10
|
+
return {
|
|
11
|
+
prepareFromDisk: (data) => {
|
|
12
|
+
const sourceText = fsSync.readFileSync(data.filePathAbsolute, "utf8");
|
|
13
|
+
const { languageFile, root } = createMarkdownFile({
|
|
14
|
+
...data,
|
|
15
|
+
sourceText
|
|
16
|
+
});
|
|
17
|
+
return prepareMarkdownFile(languageFile, root, sourceText);
|
|
18
|
+
},
|
|
19
|
+
prepareFromVirtual: (data) => {
|
|
20
|
+
const { languageFile, root } = createMarkdownFile(data);
|
|
21
|
+
return prepareMarkdownFile(languageFile, root, data.sourceText);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
export { markdownLanguage };
|
|
29
|
+
//# sourceMappingURL=language.js.map
|
package/lib/nodes.d.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import * as mdast from "mdast";
|
|
2
|
+
import { Point, Position } from "unist";
|
|
3
|
+
|
|
4
|
+
//#region src/nodes.d.ts
|
|
5
|
+
interface MarkdownNodesByName extends RootContentMapWithChildren {
|
|
6
|
+
root: WithPosition<mdast.Root>;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* A version of {@link Point} where the `offset` always exists.
|
|
10
|
+
*/
|
|
11
|
+
interface PointWithOffset extends Point {
|
|
12
|
+
offset: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A version of {@link Position} where the `end` and `start` points include offsets.
|
|
16
|
+
*/
|
|
17
|
+
interface PositionWithOffsets extends Position {
|
|
18
|
+
end: PointWithOffset;
|
|
19
|
+
start: PointWithOffset;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A version of {@link mdast.RootContentMap} that includes position information for each node.
|
|
23
|
+
*/
|
|
24
|
+
type RootContentMapWithChildren = { [K in keyof mdast.RootContentMap]: WithPosition<mdast.RootContentMap[K]> };
|
|
25
|
+
type WithPosition<T> = T & {
|
|
26
|
+
position: PositionWithOffsets;
|
|
27
|
+
};
|
|
28
|
+
//#endregion
|
|
29
|
+
export { MarkdownNodesByName, PointWithOffset, PositionWithOffsets, RootContentMapWithChildren, WithPosition };
|
|
30
|
+
//# sourceMappingURL=nodes.d.ts.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { LanguageFileDefinition } from "@flint.fyi/core";
|
|
2
|
+
import type { Root } from "mdast";
|
|
3
|
+
import type { MarkdownFileServices } from "./language.ts";
|
|
4
|
+
import type { MarkdownNodesByName } from "./nodes.ts";
|
|
5
|
+
export declare function prepareMarkdownFile(languageFile: LanguageFileDefinition<MarkdownNodesByName, MarkdownFileServices>, root: Root, sourceText: string): {
|
|
6
|
+
file: LanguageFileDefinition<MarkdownNodesByName, MarkdownFileServices>;
|
|
7
|
+
directives: import("@flint.fyi/core").CommentDirective[];
|
|
8
|
+
reports: import("@flint.fyi/core").FileReport[];
|
|
9
|
+
};
|
|
10
|
+
//# sourceMappingURL=prepareMarkdownFile.d.ts.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { parseDirectivesFromMarkdownFile } from "./directives/parseDirectivesFromMarkdownFile.js";
|
|
2
|
+
|
|
3
|
+
//#region src/prepareMarkdownFile.ts
|
|
4
|
+
function prepareMarkdownFile(languageFile, root, sourceText) {
|
|
5
|
+
return {
|
|
6
|
+
...parseDirectivesFromMarkdownFile(root, sourceText),
|
|
7
|
+
file: languageFile
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
//#endregion
|
|
12
|
+
export { prepareMarkdownFile };
|
|
13
|
+
//# sourceMappingURL=prepareMarkdownFile.js.map
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@flint.fyi/markdown-language",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "[Experimental] Markdown language for Flint.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/flint-fyi/flint.git",
|
|
8
|
+
"directory": "packages/markdown-language"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"author": {
|
|
12
|
+
"name": "JoshuaKGoldberg",
|
|
13
|
+
"email": "npm@joshuakgoldberg.com"
|
|
14
|
+
},
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"type": "module",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"@flint.fyi/source": "./src/index.ts",
|
|
20
|
+
"default": "./lib/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"lib/",
|
|
25
|
+
"!lib/**/*.map"
|
|
26
|
+
],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "vitest --typecheck --project markdown-language"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@flint.fyi/core": "workspace:^",
|
|
32
|
+
"@flint.fyi/utils": "workspace:^",
|
|
33
|
+
"mdast-util-from-markdown": "^2.0.2",
|
|
34
|
+
"mdast-util-gfm": "^3.1.0",
|
|
35
|
+
"micromark-extension-gfm": "^3.0.0",
|
|
36
|
+
"unist-util-visit": "^5.0.0"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/unist": "3.0.3",
|
|
40
|
+
"tsdown": "0.19.0-beta.2",
|
|
41
|
+
"vitest": "4.0.15"
|
|
42
|
+
},
|
|
43
|
+
"engines": {
|
|
44
|
+
"node": ">=24.0.0"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"access": "public",
|
|
48
|
+
"exports": {
|
|
49
|
+
".": "./lib/index.js"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|