sveldoc
Version:
Readme-driven Development for building Svelte components
153 lines (151 loc) • 7.32 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.processReadme = void 0;
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const marked_1 = require("marked");
const prettier_1 = __importDefault(require("prettier"));
require("prettier-plugin-svelte");
const prismjs_1 = __importDefault(require("prismjs"));
require("prismjs/components/prism-bash");
require("prism-svelte");
const extract_component_path_1 = require("./utils/extract-component-path");
const extract_component_options_1 = require("./utils/extract-component-options");
const match_1 = require("./utils/match");
const parse_component_1 = require("./utils/parse-component");
const get_package_json_1 = require("./utils/get-package-json");
const package_json = (0, get_package_json_1.getPackageJson)();
const processReadme = (options) => __awaiter(void 0, void 0, void 0, function* () {
var e_1, _a;
var _b, _c;
const { source, filename, noEval, branch = "master" } = options;
const base_url = (_b = options === null || options === void 0 ? void 0 : options.base) !== null && _b !== void 0 ? _b : "./";
marked_1.marked.use({
highlight: (code, lang) => {
let highlighted = code;
try {
highlighted = prismjs_1.default.highlight(highlighted, prismjs_1.default.languages[lang], lang);
}
catch (e) {
}
finally {
return highlighted;
}
},
walkTokens: (token) => {
var _a;
if (token.type === "link" &&
/^\.\//.test(token.href) &&
((_a = package_json === null || package_json === void 0 ? void 0 : package_json.repository) === null || _a === void 0 ? void 0 : _a.url)) {
token.href = node_path_1.default.join(package_json.repository.url, "tree", branch, token.href);
}
},
});
let cleaned = "";
let open = false;
source.split("\n").forEach((line, index, lines) => {
if (!open) {
cleaned += line + "\n";
open = match_1.match.exampleStart(line);
}
if (match_1.match.exampleEnd(lines[index + 1]))
open = false;
});
if (!noEval) {
cleaned = marked_1.marked.parse(cleaned);
}
let dependencies = new Set();
let lines_code = "";
try {
for (var _d = __asyncValues(cleaned.split("\n")), _e; _e = yield _d.next(), !_e.done;) {
let line = _e.value;
lines_code += line + "\n";
if (match_1.match.exampleStart(line)) {
const path_component = (0, extract_component_path_1.extractComponentPath)(line);
const path_options = (0, extract_component_options_1.extractComponentOptions)(line);
if (!path_component)
continue;
if (!node_fs_1.default.existsSync(path_component))
continue;
dependencies.add(node_path_1.default.resolve(path_component));
// Create corresponding `.html` file for component
const { base, name } = node_path_1.default.parse(path_component);
const template = `
<script type="module">
import Component from "./${base}";
new Component({ target: document.body });
</script>
`;
const path_html = node_path_1.default.resolve(path_component + ".html");
if (!node_fs_1.default.existsSync(path_html)) {
node_fs_1.default.writeFileSync(path_html, template);
}
const source = node_fs_1.default.readFileSync(path_component, "utf-8");
const { html, css } = (yield (0, parse_component_1.parseComponent)({ source, filename })).parsed;
let source_modified = path_options.blocks !== null ? "" : source;
(_c = path_options.blocks) === null || _c === void 0 ? void 0 : _c.forEach((block) => {
if (block === "markup") {
source_modified += html;
}
if (block === "style" && css) {
source_modified += `<style>\n${css}\n<\/style>`;
}
});
const source_formatted = prettier_1.default.format(source_modified, {
parser: "svelte",
});
const highlighted_code = prismjs_1.default.highlight(source_formatted, prismjs_1.default.languages.svelte, "svelte");
let line_modified = "\n";
if (noEval) {
line_modified += `\`\`\`svelte\n${source_formatted}\`\`\``;
}
else {
if (!path_options.no_eval) {
line_modified += `
<iframe
title="${name} example"
src="${base_url}examples/${base}.html"
loading="lazy"
${path_options.height
? `style='height: ${path_options.height}'`
: ""}
/>`;
}
line_modified += `<pre><code>{@html \`${highlighted_code}\`}</code></pre>\n\n`;
}
lines_code += line_modified + "\n\n";
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_e && !_e.done && (_a = _d.return)) yield _a.call(_d);
}
finally { if (e_1) throw e_1.error; }
}
return {
code: lines_code,
dependencies: [...dependencies],
};
});
exports.processReadme = processReadme;
;