@animode/internal 1.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/common/marked.js +22 -0
- package/index.d.ts +4 -0
- package/package.json +24 -0
- package/rollup/typescript.mjs +26 -0
- package/transformers/markdown-literals.js +65 -0
- package/vite/markdown-literals.js +10 -0
package/common/marked.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const {Marked} = require('marked');
|
|
2
|
+
const highlightJs = require('highlight.js');
|
|
3
|
+
|
|
4
|
+
module.exports = new Marked({
|
|
5
|
+
renderer: {
|
|
6
|
+
link(href, title, text) {
|
|
7
|
+
return `<a href='${href}' target='_blank'>${text}</a>`;
|
|
8
|
+
},
|
|
9
|
+
code(code, info) {
|
|
10
|
+
const [lang, ...rest] = (info || '').split(/\s+/);
|
|
11
|
+
code = code
|
|
12
|
+
.split('\n')
|
|
13
|
+
.filter(line => !line.includes('prettier-ignore'))
|
|
14
|
+
.join('\n');
|
|
15
|
+
const language = highlightJs.getLanguage(lang) ? lang : 'plaintext';
|
|
16
|
+
const result = highlightJs.highlight(code, {language});
|
|
17
|
+
return `<pre class="${rest.join(
|
|
18
|
+
' ',
|
|
19
|
+
)}"><code class="language-${language}">${result.value}</code></pre>`;
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
});
|
package/index.d.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@animode/internal",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Internal helper package for Animode bundler and transformer integrations",
|
|
5
|
+
"main": "index.d.ts",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.d.ts",
|
|
9
|
+
"common",
|
|
10
|
+
"rollup",
|
|
11
|
+
"transformers",
|
|
12
|
+
"vite"
|
|
13
|
+
],
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@rollup/plugin-commonjs": "^25.0.7",
|
|
16
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
17
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
18
|
+
"@rollup/plugin-typescript": "^11.1.5",
|
|
19
|
+
"highlight.js": "^11.9.0",
|
|
20
|
+
"marked": "^10.0.0",
|
|
21
|
+
"rollup-plugin-postcss": "^4.0.2",
|
|
22
|
+
"ts-patch": "^3.0.2"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import typescriptBase from '@rollup/plugin-typescript';
|
|
2
|
+
import markdownLiterals from '../transformers/markdown-literals.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {import('@rollup/plugin-typescript').RollupTypescriptOptions} options
|
|
6
|
+
* @returns {Plugin}
|
|
7
|
+
*/
|
|
8
|
+
export default function typescript(options = {}) {
|
|
9
|
+
const {compilerOptions, ...rest} = options;
|
|
10
|
+
return typescriptBase({
|
|
11
|
+
compilerOptions: {
|
|
12
|
+
declaration: false,
|
|
13
|
+
declarationMap: false,
|
|
14
|
+
...(compilerOptions ?? {}),
|
|
15
|
+
},
|
|
16
|
+
transformers: {
|
|
17
|
+
before: [
|
|
18
|
+
{
|
|
19
|
+
type: 'program',
|
|
20
|
+
factory: markdownLiterals,
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
},
|
|
24
|
+
...rest,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
const ts = require('typescript');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const marked = require('../common/marked');
|
|
5
|
+
|
|
6
|
+
const transformerProgram = program => context => sourceFile => {
|
|
7
|
+
const sourceMap = new Map();
|
|
8
|
+
const visitor = node => {
|
|
9
|
+
if (
|
|
10
|
+
(node.kind === ts.SyntaxKind.NoSubstitutionTemplateLiteral ||
|
|
11
|
+
ts.isStringLiteral(node)) &&
|
|
12
|
+
node.getFullText().trim().startsWith('// language=markdown')
|
|
13
|
+
) {
|
|
14
|
+
return ts.factory.createStringLiteral(marked.parse(node.text));
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (
|
|
18
|
+
ts.isImportDeclaration(node) &&
|
|
19
|
+
ts.isStringLiteral(node.moduleSpecifier) &&
|
|
20
|
+
node.importClause?.name !== undefined
|
|
21
|
+
) {
|
|
22
|
+
/**
|
|
23
|
+
* @type {ts.TypeChecker}
|
|
24
|
+
*/
|
|
25
|
+
const typeChecker = program.getTypeChecker();
|
|
26
|
+
const moduleSymbol = typeChecker.getSymbolAtLocation(
|
|
27
|
+
node.moduleSpecifier,
|
|
28
|
+
);
|
|
29
|
+
if (moduleSymbol?.escapedName !== '"*.md"') {
|
|
30
|
+
return node;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const baseDir = path.dirname(sourceFile.fileName);
|
|
34
|
+
const content = marked.parse(
|
|
35
|
+
fs.readFileSync(
|
|
36
|
+
path.resolve(baseDir, node.moduleSpecifier.text),
|
|
37
|
+
'utf-8',
|
|
38
|
+
),
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const variableSymbol = typeChecker.getSymbolAtLocation(
|
|
42
|
+
node.importClause.name,
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
sourceMap.set(variableSymbol, content);
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (ts.isIdentifier(node)) {
|
|
50
|
+
const typeChecker = program.getTypeChecker();
|
|
51
|
+
const symbol = typeChecker.getSymbolAtLocation(node);
|
|
52
|
+
if (sourceMap.has(symbol)) {
|
|
53
|
+
return ts.factory.createStringLiteral(sourceMap.get(symbol));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return node;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return ts.visitEachChild(node, visitor, context);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
return ts.visitNode(sourceFile, visitor);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
module.exports = transformerProgram;
|