md-hopper 1.0.0-alpha → 1.0.0-alpha.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/README.md +1 -1
- package/dist/_internal/types/md.js +1 -1
- package/dist/_internal/utils/md/lexer.d.ts +1 -0
- package/dist/_internal/utils/md/lexer.js +5 -0
- package/dist/_internal/utils/md/line.d.ts +22 -0
- package/dist/_internal/utils/md/line.js +51 -0
- package/dist/_internal/utils/md.d.ts +2 -3
- package/dist/_internal/utils/md.js +15 -23
- package/package.json +8 -4
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ export const PARAM_SEPARATOR = ':';
|
|
|
14
14
|
export const MD_HOPPER_COMMENT_PREFIX = 'MD_HOPPER';
|
|
15
15
|
export const COMMENT_REG_EXP = new RegExp(`${BEGIN_COMMENT}\\s*.*?${END_COMMENT}`, 'gs');
|
|
16
16
|
export const BRAKE_LINE_REG_EXP = new RegExp(/\r?\n/, 'g');
|
|
17
|
-
export const LINK_REG_EXP = new RegExp('
|
|
17
|
+
export const LINK_REG_EXP = new RegExp('^(\\s*.*)\\[(.+)][(|\\[](.+)[)|\\]].*');
|
|
18
18
|
export const MD_HOPPER_COMMENT_PREFIX_REG_EXP = new RegExp(`${BEGIN_COMMENT}\\s*${MD_HOPPER_COMMENT_PREFIX}${PARAM_SEPARATOR}\\s*`);
|
|
19
19
|
export const MD_HOPPER_PARAMS_REG_EXP = new RegExp(`^[^${PARAM_SEPARATOR}]+${PARAM_SEPARATOR}(.*?)${END_COMMENT}.*`, 's');
|
|
20
20
|
export const MD_HOPPER_CONTENT_REG_EXP = new RegExp(`^[^${PARAM_SEPARATOR}]+${PARAM_SEPARATOR}.*${END_COMMENT}(.*)`, 's');
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const lexer: (txt: string) => import("marked").Token;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Token as _Token, Tokens } from 'marked';
|
|
2
|
+
type Token = Tokens.ListItem | Tokens.Heading;
|
|
3
|
+
interface SymbolEl {
|
|
4
|
+
raw: Token['raw'];
|
|
5
|
+
text: Token['text'];
|
|
6
|
+
}
|
|
7
|
+
export declare const getSymbolAndText: ({ raw, text }: SymbolEl) => {
|
|
8
|
+
symbol: string;
|
|
9
|
+
text: string;
|
|
10
|
+
};
|
|
11
|
+
export declare const getElements: (txt: string) => {
|
|
12
|
+
indent: string;
|
|
13
|
+
str: string;
|
|
14
|
+
token: _Token;
|
|
15
|
+
};
|
|
16
|
+
export declare const getProperties: (txt: string) => {
|
|
17
|
+
indent: string;
|
|
18
|
+
symbol: string;
|
|
19
|
+
text: string;
|
|
20
|
+
} | null;
|
|
21
|
+
export declare const replaceLineToLink: (txt: string, defineName: string) => string;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { lexer } from './lexer.js';
|
|
2
|
+
const TOKEN_TYPES = {
|
|
3
|
+
list: 'list',
|
|
4
|
+
heading: 'heading',
|
|
5
|
+
};
|
|
6
|
+
export const getSymbolAndText = ({ raw, text }) => {
|
|
7
|
+
return {
|
|
8
|
+
symbol: raw.replace(new RegExp(`${text}$`), ''),
|
|
9
|
+
text,
|
|
10
|
+
};
|
|
11
|
+
};
|
|
12
|
+
export const getElements = (txt) => {
|
|
13
|
+
const indent = txt.replace(/^(\s*)(.*)$/, '$1');
|
|
14
|
+
const str = txt.replace(/^(\s*)(.*)$/, '$2');
|
|
15
|
+
const token = lexer(str);
|
|
16
|
+
return {
|
|
17
|
+
indent,
|
|
18
|
+
str,
|
|
19
|
+
token,
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
export const getProperties = (txt) => {
|
|
23
|
+
const { indent, token } = getElements(txt);
|
|
24
|
+
let symbol = '';
|
|
25
|
+
let text = '';
|
|
26
|
+
switch (token.type) {
|
|
27
|
+
case TOKEN_TYPES.list: {
|
|
28
|
+
const symbolAndText = getSymbolAndText(token.items[0]);
|
|
29
|
+
symbol = symbolAndText.symbol;
|
|
30
|
+
text = symbolAndText.text;
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
case TOKEN_TYPES.heading: {
|
|
34
|
+
const symbolAndText = getSymbolAndText(token);
|
|
35
|
+
symbol = symbolAndText.symbol;
|
|
36
|
+
text = symbolAndText.text;
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
default:
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
return { indent, symbol, text };
|
|
43
|
+
};
|
|
44
|
+
export const replaceLineToLink = (txt, defineName) => {
|
|
45
|
+
const properties = getProperties(txt);
|
|
46
|
+
if (!properties) {
|
|
47
|
+
return `[${txt}][${defineName}]`;
|
|
48
|
+
}
|
|
49
|
+
const { indent, symbol, text } = properties;
|
|
50
|
+
return `${indent}${symbol}[${text}][${defineName}]`;
|
|
51
|
+
};
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Detail } from '../types/md.js';
|
|
2
|
-
export declare const getParam: (
|
|
3
|
-
export declare const
|
|
4
|
-
export declare const getTitle: (text: string) => string;
|
|
2
|
+
export declare const getParam: (txt: string, key: string) => string;
|
|
3
|
+
export declare const getTitle: (txt: string) => string;
|
|
5
4
|
export declare const replace: (detail: Detail, details: Detail[]) => string;
|
|
@@ -1,35 +1,28 @@
|
|
|
1
1
|
import { readFileSync } from 'fs';
|
|
2
2
|
import { PROPERTIES, LINK_NAME_PREFIX, COMMENT_REG_EXP, PARAM_SEPARATOR, LINK_REG_EXP, MD_HOPPER_COMMENT_PREFIX, MD_HOPPER_COMMENT_PREFIX_REG_EXP, MD_HOPPER_PROPERTY_REG_EXP, MD_HOPPER_PARAMS_REG_EXP, BRAKE_LINE_REG_EXP, MD_HOPPER_CONTENT_REG_EXP, BEGIN_COMMENT, END_COMMENT, } from '../types/md.js';
|
|
3
|
+
import { replaceLineToLink } from './md/line.js';
|
|
3
4
|
import { getRelative } from './path.js';
|
|
4
5
|
import { isBoolean } from './type-check.js';
|
|
5
|
-
export const getParam = (
|
|
6
|
+
export const getParam = (txt, key) => {
|
|
6
7
|
const PREFIX = `${BEGIN_COMMENT}\\s*${MD_HOPPER_COMMENT_PREFIX}:\\s*${key}:`;
|
|
7
8
|
const reg = new RegExp(`${PREFIX}(.*?)${END_COMMENT}`, 's');
|
|
8
|
-
if (!reg.test(
|
|
9
|
+
if (!reg.test(txt)) {
|
|
9
10
|
return '';
|
|
10
11
|
}
|
|
11
|
-
return reg.exec(
|
|
12
|
+
return reg.exec(txt)[1];
|
|
12
13
|
};
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
if (!reg.test(text)) {
|
|
17
|
-
return '';
|
|
18
|
-
}
|
|
19
|
-
return text.match(reg);
|
|
20
|
-
};
|
|
21
|
-
const getRemovedCommentOut = (text) => text.replace(COMMENT_REG_EXP, '');
|
|
22
|
-
const getFirstLine = (text) => {
|
|
23
|
-
const lines = text.split('\n').filter(line => line.trim().length);
|
|
14
|
+
const getRemovedComment = (txt) => txt.replace(COMMENT_REG_EXP, '');
|
|
15
|
+
const getFirstLine = (txt) => {
|
|
16
|
+
const lines = txt.split('\n').filter(line => line.trim().length);
|
|
24
17
|
return lines.length ? lines[0].trim() : '';
|
|
25
18
|
};
|
|
26
|
-
export const getTitle = (
|
|
27
|
-
const title = getParam(
|
|
19
|
+
export const getTitle = (txt) => {
|
|
20
|
+
const title = getParam(txt, 'TITLE');
|
|
28
21
|
if (title) {
|
|
29
22
|
return title;
|
|
30
23
|
}
|
|
31
|
-
const md =
|
|
32
|
-
return
|
|
24
|
+
const md = getRemovedComment(txt);
|
|
25
|
+
return md.replace(/\r?\n?#+(.*?)\r?\n.*/s, '$1').trim() || getFirstLine(md);
|
|
33
26
|
};
|
|
34
27
|
const getLinkName = (id) => `${LINK_NAME_PREFIX}${id}`;
|
|
35
28
|
const getLink = (id, title) => `[${title}][${getLinkName(id)}]`;
|
|
@@ -37,17 +30,16 @@ const getLinksByDetails = (details) => details.map(d => getLink(d.id, d.title));
|
|
|
37
30
|
const getDefineLink = (id, url, title) => `[${getLinkName(id)}]: ${url}${title ? ` '${title}'` : ''}`;
|
|
38
31
|
const replaceToLink = (txt, inline, detail) => {
|
|
39
32
|
if (LINK_REG_EXP.test(txt)) {
|
|
40
|
-
const url = txt.replace(LINK_REG_EXP, '$
|
|
33
|
+
const url = txt.replace(LINK_REG_EXP, '$3');
|
|
41
34
|
if (url === getLinkName(detail.id)) {
|
|
42
35
|
return txt;
|
|
43
36
|
}
|
|
44
|
-
return
|
|
45
|
-
<!-- ${MD_HOPPER_COMMENT_PREFIX}${PARAM_SEPARATOR} ${PROPERTIES.BEFORE_GENERATE_LINK}${PARAM_SEPARATOR} -->
|
|
46
|
-
<!-- ${txt} -->
|
|
37
|
+
return `${replaceLineToLink(txt.replace(LINK_REG_EXP, '$1$2'), getLinkName(detail.id))}
|
|
38
|
+
<!-- ${MD_HOPPER_COMMENT_PREFIX}${PARAM_SEPARATOR} ${PROPERTIES.BEFORE_GENERATE_LINK} (${new Date().toLocaleString()})${PARAM_SEPARATOR} ${txt} -->
|
|
47
39
|
`;
|
|
48
40
|
}
|
|
49
41
|
if (inline) {
|
|
50
|
-
return
|
|
42
|
+
return replaceLineToLink(txt, getLinkName(detail.id));
|
|
51
43
|
}
|
|
52
44
|
else {
|
|
53
45
|
return `[${detail.title}][${getLinkName(detail.id)}]
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "md-hopper",
|
|
3
|
-
"version": "1.0.0-alpha",
|
|
3
|
+
"version": "1.0.0-alpha.1",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -16,6 +16,12 @@
|
|
|
16
16
|
"url": "https://github.com/cilly-yllic/md-hopper/issues"
|
|
17
17
|
},
|
|
18
18
|
"homepage": "https://github.com/cilly-yllic/md-hopper#readme",
|
|
19
|
+
"keywords": [
|
|
20
|
+
"markdown",
|
|
21
|
+
"readme",
|
|
22
|
+
"md",
|
|
23
|
+
"links"
|
|
24
|
+
],
|
|
19
25
|
"scripts": {
|
|
20
26
|
"prepare": "husky",
|
|
21
27
|
"jest": "jest",
|
|
@@ -30,20 +36,17 @@
|
|
|
30
36
|
"generate": "tsx --tsconfig ./tsconfig.zx.json ./tools/generate-alias.zx.ts",
|
|
31
37
|
"ci:update-version": "tsx --tsconfig ./tsconfig.zx.json ./tools/update-version.zx.ts",
|
|
32
38
|
"md:generate": "tsx --tsconfig ./tsconfig.zx.json ./tools/generate-md.ts",
|
|
33
|
-
|
|
34
39
|
"//--- test ---//": "",
|
|
35
40
|
"test": "run-s test:*",
|
|
36
41
|
"test:lint": "npm run lint",
|
|
37
42
|
"test:prettier": "npm run prettier:check",
|
|
38
43
|
"test:jest": "npm run jest",
|
|
39
|
-
|
|
40
44
|
"//--- build ---//": "",
|
|
41
45
|
"build": "run-s build:*",
|
|
42
46
|
"build:test": "npm run test",
|
|
43
47
|
"build:clean": "npm run clean",
|
|
44
48
|
"build:tsc": "npm run tsc",
|
|
45
49
|
"build:generate": "npm run generate",
|
|
46
|
-
|
|
47
50
|
"//--- publish ---//": "",
|
|
48
51
|
"publish:build": "run-s build:{clean,tsc,generate}"
|
|
49
52
|
},
|
|
@@ -61,6 +64,7 @@
|
|
|
61
64
|
"@actions/github": "^6.0.0",
|
|
62
65
|
"chalk": "^5.3.0",
|
|
63
66
|
"commander": "^12.0.0",
|
|
67
|
+
"marked": "^12.0.2",
|
|
64
68
|
"minimatch": "^9.0.4",
|
|
65
69
|
"my-gadgetry": "^1.1.2",
|
|
66
70
|
"zx": "^8.1.0"
|