gatsby-source-tilda
Version:
Gatsby source plugin for building websites using the tilda.cc CMS as a data source
82 lines (81 loc) • 2.99 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTildaPageAssets = exports.createTildaPages = void 0;
const progress_1 = __importDefault(require("progress"));
const consts_1 = require("./consts");
const utils_1 = require("./utils");
const createTildaPageAsset = async (gatsbyFunctions, asset) => {
const { actions: { createNode }, createNodeId, createContentDigest, } = gatsbyFunctions;
const nodeId = createNodeId(`tilda-page-asset-${asset.pageId}-${asset.from}`);
const nodeContent = JSON.stringify(asset);
const nodeData = {
...asset,
id: nodeId,
parent: null,
children: [],
internal: {
type: consts_1.PAGE_ASSET_TYPE,
content: nodeContent,
contentDigest: createContentDigest(asset.from),
},
};
await createNode(nodeData);
return nodeData;
};
const createTildaPage = async (gatsbyFunctions, page) => {
const { actions: { createNode }, createNodeId, createContentDigest, } = gatsbyFunctions;
const nodeId = (0, utils_1.createPageNodeId)(createNodeId, page.id);
let { html } = page;
const initScripts = html.match(/<script[\s\S]*?>[\s\S]*?<\/script>/gi) || [];
initScripts.forEach((script) => {
html = html.replace(script, '');
});
const nodeContent = JSON.stringify(page);
const nodeData = {
...page,
initScripts,
html,
pageId: page.id,
id: nodeId,
parent: null,
children: [],
internal: {
type: consts_1.PAGE_TYPE,
content: nodeContent,
contentDigest: createContentDigest(html),
},
};
return createNode(nodeData);
};
const createTildaPages = (gatsbyFunctions, pluginConfig, pages) => {
// create nodes for all tilda pages
const progressBar = new progress_1.default('Creating TildaPage Nodes [:bar] :current/:total :elapsed secs :percent', {
total: pages.length,
width: 30,
});
const createNodePromises = [];
pages.forEach((page) => {
const node = createTildaPage(gatsbyFunctions, page);
createNodePromises.push(node);
progressBar.tick(0, 0);
});
return createNodePromises;
};
exports.createTildaPages = createTildaPages;
const createTildaPageAssets = (gatsbyFunctions, assets) => {
const progressBar = new progress_1.default('Creating TildaPageAsset Nodes [:bar] :current/:total :elapsed secs :percent', {
total: assets.length,
width: 30,
});
const createNodePromises = [];
assets.forEach((asset) => {
const node = createTildaPageAsset(gatsbyFunctions, asset);
createNodePromises.push(node);
progressBar.tick(0, 0);
});
return createNodePromises;
};
exports.createTildaPageAssets = createTildaPageAssets;