Comparing version
import { Root } from "mdast"; | ||
/** | ||
* Find all codeblocks like: | ||
* | ||
* ```jsx | ||
* !from ./foo/bar.js | ||
* ``` | ||
* and replace the value with the content of the referenced file. | ||
* Find all codeblocks that contain lines starting with !from | ||
* and replace those lines with the content from the referenced files. | ||
*/ | ||
@@ -10,0 +6,0 @@ export declare function transformImportedCode(tree: Root, file?: { |
import { visit } from "unist-util-visit"; | ||
/** | ||
* Find all codeblocks like: | ||
* | ||
* ```jsx | ||
* !from ./foo/bar.js | ||
* ``` | ||
* and replace the value with the content of the referenced file. | ||
* Find all codeblocks that contain lines starting with !from | ||
* and replace those lines with the content from the referenced files. | ||
*/ | ||
@@ -13,4 +9,4 @@ export async function transformImportedCode(tree, file) { | ||
visit(tree, "code", (node) => { | ||
var _a; | ||
if ((_a = node.value) === null || _a === void 0 ? void 0 : _a.startsWith("!from ")) { | ||
var _a, _b; | ||
if (((_a = node.value) === null || _a === void 0 ? void 0 : _a.includes("\n!from ")) || ((_b = node.value) === null || _b === void 0 ? void 0 : _b.startsWith("!from "))) { | ||
nodes.push(node); | ||
@@ -24,5 +20,12 @@ } | ||
await Promise.all(nodes.map(async (code) => { | ||
const fromData = code.value.slice(6).trim(); | ||
const [codepath, range] = (fromData === null || fromData === void 0 ? void 0 : fromData.split(/\s+/)) || []; | ||
code.value = await readFile(codepath, mdxPath, range); | ||
const lines = code.value.split("\n"); | ||
const newLines = await Promise.all(lines.map(async (line) => { | ||
if (line.startsWith("!from ")) { | ||
const fromData = line.slice(6).trim(); | ||
const [codepath, range] = (fromData === null || fromData === void 0 ? void 0 : fromData.split(/\s+/)) || []; | ||
return await readFile(codepath, mdxPath, range); | ||
} | ||
return line; | ||
})); | ||
code.value = newLines.join("\n"); | ||
})); | ||
@@ -29,0 +32,0 @@ return tree; |
{ | ||
"name": "codehike", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Build rich content websites with Markdown and React", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
119375
0.29%2561
-0.04%