@salesforce/pwa-kit-mcp
Version:
MCP server that helps you build Salesforce Commerce Cloud PWA Kit Composable Storefront
84 lines (80 loc) • 3.49 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.hooksCatalogAsJson = hooksCatalogAsJson;
exports.loadHooksCatalog = loadHooksCatalog;
var _promises = _interopRequireDefault(require("fs/promises"));
var _path = _interopRequireDefault(require("path"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; } /*
* Copyright (c) 2025, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
/**
* Converts markdown to JSON format for sample data.
*
* @description
* The input markdown file only supports level 3 sections (3 #) and fenced code blocks (3 backticks).
* - Any text before the first section is ignored including code blocks.
* - It supports one code block per section.
* - The code block programming language is ignored (text after the first 3 backticks up to the first new line).
* - Any text after a code block is ignored.
*
* @param {string} markdown - The markdown content to convert
*
* @returns {Array<{name: string, summary: string, snippet: string}>} An array of objects with the following structure:
* - `name`: section header
* - `summary`: section text
* - `snippet`: section code block
*
* @example
* const markdown = `
* ### Example Section
* This is a summary
* \`\`\`javascript
* const code = 'example';
* \`\`\`
* `;
* const result = hooksCatalogAsJson(markdown);
* // Returns: [{ name: 'Example Section', summary: 'This is a summary', snippet: "const code = 'example';" }]
*/
function hooksCatalogAsJson(markdown) {
const sections = markdown.split('### ').slice(1);
return sections.map(section => {
const [beforeCode, codeBlock] = section.split('```');
// Extract name (first line) and summary (rest) from before code
const firstNewline = beforeCode.indexOf('\n');
const name = beforeCode.substring(0, firstNewline).trim();
const summary = beforeCode.substring(firstNewline + 1).trim();
// Extract snippet from code block (skip first line which is the language)
let snippet = '';
if (codeBlock) {
const codeFirstNewline = codeBlock.indexOf('\n');
snippet = codeBlock.substring(codeFirstNewline + 1).trim();
}
return {
name,
summary,
snippet
};
});
}
/**
* Load the hook catalog from the given path or the default path.
* @returns {Promise<Array<{name: string, summary: string, snippet: string}>>}
*/
function loadHooksCatalog() {
return _loadHooksCatalog.apply(this, arguments);
}
function _loadHooksCatalog() {
_loadHooksCatalog = _asyncToGenerator(function* () {
const resolvedCatalogPath = _path.default.resolve(__dirname, '../data/hook-catalog.md');
const catalogRaw = yield _promises.default.readFile(resolvedCatalogPath, 'utf8');
return hooksCatalogAsJson(catalogRaw);
});
return _loadHooksCatalog.apply(this, arguments);
}