@jianwen-lang/parser 0.1.1
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/LICENSE +21 -0
- package/README.md +95 -0
- package/dist/cli/render.d.ts +1 -0
- package/dist/cli/render.js +300 -0
- package/dist/core/ast.d.ts +286 -0
- package/dist/core/ast.js +2 -0
- package/dist/core/block/rules/heading.d.ts +9 -0
- package/dist/core/block/rules/heading.js +75 -0
- package/dist/core/block/rules/horizontal-rule.d.ts +9 -0
- package/dist/core/block/rules/horizontal-rule.js +87 -0
- package/dist/core/block/rules/include.d.ts +9 -0
- package/dist/core/block/rules/include.js +64 -0
- package/dist/core/block/rules/index.d.ts +2 -0
- package/dist/core/block/rules/index.js +16 -0
- package/dist/core/block/rules/types.d.ts +21 -0
- package/dist/core/block/rules/types.js +2 -0
- package/dist/core/block/types.d.ts +10 -0
- package/dist/core/block/types.js +2 -0
- package/dist/core/block-parser.d.ts +3 -0
- package/dist/core/block-parser.js +1385 -0
- package/dist/core/clone.d.ts +9 -0
- package/dist/core/clone.js +32 -0
- package/dist/core/diagnostics.d.ts +12 -0
- package/dist/core/diagnostics.js +24 -0
- package/dist/core/errors.d.ts +12 -0
- package/dist/core/errors.js +2 -0
- package/dist/core/inline/rules/backtick.d.ts +4 -0
- package/dist/core/inline/rules/backtick.js +90 -0
- package/dist/core/inline/rules/bracket.d.ts +4 -0
- package/dist/core/inline/rules/bracket.js +721 -0
- package/dist/core/inline/rules/disabled.d.ts +2 -0
- package/dist/core/inline/rules/disabled.js +34 -0
- package/dist/core/inline/rules/escape.d.ts +2 -0
- package/dist/core/inline/rules/escape.js +11 -0
- package/dist/core/inline/rules/index.d.ts +2 -0
- package/dist/core/inline/rules/index.js +25 -0
- package/dist/core/inline/rules/marker-highlight.d.ts +4 -0
- package/dist/core/inline/rules/marker-highlight.js +56 -0
- package/dist/core/inline/rules/style.d.ts +4 -0
- package/dist/core/inline/rules/style.js +120 -0
- package/dist/core/inline/rules/types.d.ts +22 -0
- package/dist/core/inline/rules/types.js +2 -0
- package/dist/core/inline/types.d.ts +1 -0
- package/dist/core/inline/types.js +2 -0
- package/dist/core/inline-parser.d.ts +3 -0
- package/dist/core/inline-parser.js +52 -0
- package/dist/core/location.d.ts +9 -0
- package/dist/core/location.js +30 -0
- package/dist/core/parser.d.ts +9 -0
- package/dist/core/parser.js +423 -0
- package/dist/core/traverse.d.ts +7 -0
- package/dist/core/traverse.js +71 -0
- package/dist/html/convert.d.ts +29 -0
- package/dist/html/convert.js +61 -0
- package/dist/html/format.d.ts +1 -0
- package/dist/html/format.js +17 -0
- package/dist/html/render/blocks.d.ts +4 -0
- package/dist/html/render/blocks.js +433 -0
- package/dist/html/render/html.d.ts +8 -0
- package/dist/html/render/html.js +89 -0
- package/dist/html/render/inlines.d.ts +4 -0
- package/dist/html/render/inlines.js +110 -0
- package/dist/html/render/meta.d.ts +3 -0
- package/dist/html/render/meta.js +51 -0
- package/dist/html/render/utils.d.ts +31 -0
- package/dist/html/render/utils.js +213 -0
- package/dist/html/theme/base/css.d.ts +2 -0
- package/dist/html/theme/base/css.js +383 -0
- package/dist/html/theme/dark/css.d.ts +2 -0
- package/dist/html/theme/dark/css.js +108 -0
- package/dist/html/theme/default/colors.d.ts +13 -0
- package/dist/html/theme/default/colors.js +2 -0
- package/dist/html/theme/default/css.d.ts +2 -0
- package/dist/html/theme/default/css.js +6 -0
- package/dist/html/theme/default/runtime.d.ts +0 -0
- package/dist/html/theme/default/runtime.js +31 -0
- package/dist/html/theme/light/css.d.ts +2 -0
- package/dist/html/theme/light/css.js +55 -0
- package/dist/html/theme/preset/colors.d.ts +10 -0
- package/dist/html/theme/preset/colors.js +14 -0
- package/dist/html/theme/runtime.d.ts +0 -0
- package/dist/html/theme/runtime.js +31 -0
- package/dist/html/theme/theme.d.ts +9 -0
- package/dist/html/theme/theme.js +21 -0
- package/dist/lexer/lexer.d.ts +17 -0
- package/dist/lexer/lexer.js +47 -0
- package/dist/parser.d.ts +6 -0
- package/dist/parser.js +22 -0
- package/package.json +50 -0
package/dist/core/ast.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BlockRule } from './types';
|
|
2
|
+
interface HeadingMatchResult {
|
|
3
|
+
level: 1 | 2 | 3 | 4 | 5;
|
|
4
|
+
foldable?: boolean;
|
|
5
|
+
text: string;
|
|
6
|
+
}
|
|
7
|
+
export declare function matchHeading(content: string): HeadingMatchResult | undefined;
|
|
8
|
+
export declare const tryParseHeading: BlockRule;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tryParseHeading = void 0;
|
|
4
|
+
exports.matchHeading = matchHeading;
|
|
5
|
+
const location_1 = require("../../location");
|
|
6
|
+
function matchHeading(content) {
|
|
7
|
+
const foldableMatch = content.match(/^(#{1,5})\+\s+(.+)$/);
|
|
8
|
+
if (foldableMatch) {
|
|
9
|
+
const hashes = foldableMatch[1];
|
|
10
|
+
const textGroup = foldableMatch[2];
|
|
11
|
+
if (hashes === undefined || textGroup === undefined) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
const text = textGroup.trimEnd();
|
|
15
|
+
const level = hashes.length;
|
|
16
|
+
return { level, foldable: true, text };
|
|
17
|
+
}
|
|
18
|
+
const normalMatch = content.match(/^(#{1,5})\s+(.+)$/);
|
|
19
|
+
if (!normalMatch) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
const hashes = normalMatch[1];
|
|
23
|
+
const textGroup = normalMatch[2];
|
|
24
|
+
if (hashes === undefined || textGroup === undefined) {
|
|
25
|
+
return undefined;
|
|
26
|
+
}
|
|
27
|
+
const text = textGroup.trimEnd();
|
|
28
|
+
const level = hashes.length;
|
|
29
|
+
return { level, text };
|
|
30
|
+
}
|
|
31
|
+
const tryParseHeading = (ctx) => {
|
|
32
|
+
const headingMatch = matchHeading(ctx.lineInfo.content);
|
|
33
|
+
if (!headingMatch) {
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
const blockAttrs = ctx.buildBlockAttrs(ctx.lineInfo.tabCount);
|
|
37
|
+
let block;
|
|
38
|
+
if (ctx.pending.isDisabled) {
|
|
39
|
+
const disabled = {
|
|
40
|
+
type: 'disabledBlock',
|
|
41
|
+
raw: ctx.lineInfo.raw,
|
|
42
|
+
blockAttrs,
|
|
43
|
+
};
|
|
44
|
+
(0, location_1.setNodeLocation)(disabled, ctx.lineLocation);
|
|
45
|
+
block = disabled;
|
|
46
|
+
ctx.commitBlock(block, blockAttrs, {
|
|
47
|
+
allowTag: false,
|
|
48
|
+
postResetPending: (pending) => {
|
|
49
|
+
if (headingMatch.foldable) {
|
|
50
|
+
pending.foldNext = true;
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
const heading = {
|
|
57
|
+
type: 'heading',
|
|
58
|
+
level: headingMatch.level,
|
|
59
|
+
foldable: headingMatch.foldable,
|
|
60
|
+
children: [{ type: 'text', value: headingMatch.text }],
|
|
61
|
+
blockAttrs,
|
|
62
|
+
};
|
|
63
|
+
(0, location_1.setNodeLocation)(heading, ctx.lineLocation);
|
|
64
|
+
block = heading;
|
|
65
|
+
ctx.commitBlock(block, blockAttrs, {
|
|
66
|
+
postResetPending: (pending) => {
|
|
67
|
+
if (headingMatch.foldable) {
|
|
68
|
+
pending.foldNext = true;
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
return ctx.index + 1;
|
|
74
|
+
};
|
|
75
|
+
exports.tryParseHeading = tryParseHeading;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ColorAttribute, HorizontalRuleBlock } from '../../ast';
|
|
2
|
+
import { BlockRule } from './types';
|
|
3
|
+
interface HorizontalRuleMatchResult {
|
|
4
|
+
style: HorizontalRuleBlock['style'];
|
|
5
|
+
colorAttr?: ColorAttribute;
|
|
6
|
+
}
|
|
7
|
+
export declare function matchHorizontalRule(trimmed: string): HorizontalRuleMatchResult | undefined;
|
|
8
|
+
export declare const tryParseHorizontalRule: BlockRule;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tryParseHorizontalRule = void 0;
|
|
4
|
+
exports.matchHorizontalRule = matchHorizontalRule;
|
|
5
|
+
const location_1 = require("../../location");
|
|
6
|
+
function parseColorAttribute(token) {
|
|
7
|
+
const text = token.trim();
|
|
8
|
+
if (text.length === 0) {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
if (text.startsWith('#')) {
|
|
12
|
+
return { kind: 'hex', value: text };
|
|
13
|
+
}
|
|
14
|
+
return { kind: 'preset', value: text };
|
|
15
|
+
}
|
|
16
|
+
function matchHorizontalRule(trimmed) {
|
|
17
|
+
let rest = trimmed;
|
|
18
|
+
let colorAttr;
|
|
19
|
+
if (rest.startsWith('[')) {
|
|
20
|
+
const end = rest.indexOf(']');
|
|
21
|
+
if (end !== -1) {
|
|
22
|
+
const inside = rest.slice(1, end).trim();
|
|
23
|
+
const parsedColor = parseColorAttribute(inside);
|
|
24
|
+
if (parsedColor) {
|
|
25
|
+
colorAttr = parsedColor;
|
|
26
|
+
rest = rest.slice(end + 1).trim();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (rest.length < 5) {
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
const ch = rest[0];
|
|
34
|
+
if (ch !== '-' && ch !== '*' && ch !== '=' && ch !== '~') {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
for (let i = 1; i < rest.length; i += 1) {
|
|
38
|
+
if (rest[i] !== ch) {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
let style;
|
|
43
|
+
if (ch === '-') {
|
|
44
|
+
style = 'solid';
|
|
45
|
+
}
|
|
46
|
+
else if (ch === '*') {
|
|
47
|
+
style = 'dashed';
|
|
48
|
+
}
|
|
49
|
+
else if (ch === '=') {
|
|
50
|
+
style = 'bold';
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
style = 'wavy';
|
|
54
|
+
}
|
|
55
|
+
return { style, colorAttr };
|
|
56
|
+
}
|
|
57
|
+
const tryParseHorizontalRule = (ctx) => {
|
|
58
|
+
const hrMatch = matchHorizontalRule(ctx.trimmedContent);
|
|
59
|
+
if (!hrMatch) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
const blockAttrs = ctx.buildBlockAttrs(ctx.lineInfo.tabCount);
|
|
63
|
+
let block;
|
|
64
|
+
if (ctx.pending.isDisabled) {
|
|
65
|
+
const disabled = {
|
|
66
|
+
type: 'disabledBlock',
|
|
67
|
+
raw: ctx.lineInfo.raw,
|
|
68
|
+
blockAttrs,
|
|
69
|
+
};
|
|
70
|
+
(0, location_1.setNodeLocation)(disabled, ctx.lineLocation);
|
|
71
|
+
block = disabled;
|
|
72
|
+
ctx.commitBlock(block, blockAttrs, { allowTag: false });
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const hr = {
|
|
76
|
+
type: 'hr',
|
|
77
|
+
style: hrMatch.style,
|
|
78
|
+
colorAttr: hrMatch.colorAttr,
|
|
79
|
+
blockAttrs,
|
|
80
|
+
};
|
|
81
|
+
(0, location_1.setNodeLocation)(hr, ctx.lineLocation);
|
|
82
|
+
block = hr;
|
|
83
|
+
ctx.commitBlock(block, blockAttrs);
|
|
84
|
+
}
|
|
85
|
+
return ctx.index + 1;
|
|
86
|
+
};
|
|
87
|
+
exports.tryParseHorizontalRule = tryParseHorizontalRule;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { IncludeBlock } from '../../ast';
|
|
2
|
+
import { BlockRule } from './types';
|
|
3
|
+
interface IncludeMatchResult {
|
|
4
|
+
mode: IncludeBlock['mode'];
|
|
5
|
+
target: string;
|
|
6
|
+
}
|
|
7
|
+
export declare function matchInclude(content: string): IncludeMatchResult | undefined;
|
|
8
|
+
export declare const tryParseInclude: BlockRule;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tryParseInclude = void 0;
|
|
4
|
+
exports.matchInclude = matchInclude;
|
|
5
|
+
const location_1 = require("../../location");
|
|
6
|
+
function matchInclude(content) {
|
|
7
|
+
const trimmed = content.trim();
|
|
8
|
+
const fileMatch = trimmed.match(/^\[@\]\(([^)]+)\)\s*$/);
|
|
9
|
+
if (fileMatch) {
|
|
10
|
+
const pathGroup = fileMatch[1];
|
|
11
|
+
if (!pathGroup) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
const target = pathGroup.trim();
|
|
15
|
+
if (target.length === 0) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
return { mode: 'file', target };
|
|
19
|
+
}
|
|
20
|
+
const tagMatch = trimmed.match(/^\[@=([^\]]+)\]\s*$/);
|
|
21
|
+
if (tagMatch) {
|
|
22
|
+
const nameGroup = tagMatch[1];
|
|
23
|
+
if (!nameGroup) {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
const target = nameGroup.trim();
|
|
27
|
+
if (target.length === 0) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
return { mode: 'tag', target };
|
|
31
|
+
}
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
const tryParseInclude = (ctx) => {
|
|
35
|
+
const includeMatch = matchInclude(ctx.lineInfo.content);
|
|
36
|
+
if (!includeMatch) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
const blockAttrs = ctx.buildBlockAttrs(ctx.lineInfo.tabCount);
|
|
40
|
+
let block;
|
|
41
|
+
if (ctx.pending.isDisabled) {
|
|
42
|
+
const disabled = {
|
|
43
|
+
type: 'disabledBlock',
|
|
44
|
+
raw: ctx.lineInfo.raw,
|
|
45
|
+
blockAttrs,
|
|
46
|
+
};
|
|
47
|
+
(0, location_1.setNodeLocation)(disabled, ctx.lineLocation);
|
|
48
|
+
block = disabled;
|
|
49
|
+
ctx.commitBlock(block, blockAttrs, { allowTag: false });
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
const includeBlock = {
|
|
53
|
+
type: 'include',
|
|
54
|
+
mode: includeMatch.mode,
|
|
55
|
+
target: includeMatch.target,
|
|
56
|
+
blockAttrs,
|
|
57
|
+
};
|
|
58
|
+
(0, location_1.setNodeLocation)(includeBlock, ctx.lineLocation);
|
|
59
|
+
block = includeBlock;
|
|
60
|
+
ctx.commitBlock(block, blockAttrs);
|
|
61
|
+
}
|
|
62
|
+
return ctx.index + 1;
|
|
63
|
+
};
|
|
64
|
+
exports.tryParseInclude = tryParseInclude;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tryParseBlockRules = tryParseBlockRules;
|
|
4
|
+
const include_1 = require("./include");
|
|
5
|
+
const heading_1 = require("./heading");
|
|
6
|
+
const horizontal_rule_1 = require("./horizontal-rule");
|
|
7
|
+
const BLOCK_RULES = [include_1.tryParseInclude, heading_1.tryParseHeading, horizontal_rule_1.tryParseHorizontalRule];
|
|
8
|
+
function tryParseBlockRules(ctx) {
|
|
9
|
+
for (const rule of BLOCK_RULES) {
|
|
10
|
+
const nextIndex = rule(ctx);
|
|
11
|
+
if (nextIndex !== null) {
|
|
12
|
+
return nextIndex;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { BlockAttributes, BlockNode } from '../../ast';
|
|
2
|
+
import { ParseError } from '../../errors';
|
|
3
|
+
import { SourceLocation } from '../../location';
|
|
4
|
+
import { PendingBlockContext } from '../types';
|
|
5
|
+
import { LineInfo } from '../../../lexer/lexer';
|
|
6
|
+
export interface CommitBlockOptions {
|
|
7
|
+
allowTag?: boolean;
|
|
8
|
+
postResetPending?: (pending: PendingBlockContext) => void;
|
|
9
|
+
}
|
|
10
|
+
export interface BlockRuleContext {
|
|
11
|
+
lines: string[];
|
|
12
|
+
index: number;
|
|
13
|
+
lineInfo: LineInfo;
|
|
14
|
+
trimmedContent: string;
|
|
15
|
+
lineLocation: SourceLocation;
|
|
16
|
+
pending: PendingBlockContext;
|
|
17
|
+
errors: ParseError[];
|
|
18
|
+
buildBlockAttrs: (tabCount: number) => BlockAttributes | undefined;
|
|
19
|
+
commitBlock: (block: BlockNode, blockAttrs: BlockAttributes | undefined, options?: CommitBlockOptions) => void;
|
|
20
|
+
}
|
|
21
|
+
export type BlockRule = (ctx: BlockRuleContext) => number | null;
|