UNPKG

@jackdbd/eleventy-plugin-text-to-speech

Version:
95 lines 4.36 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.plugin = exports.mediaType = exports.audioExtension = void 0; const node_fs_1 = __importDefault(require("node:fs")); const node_path_1 = __importDefault(require("node:path")); const debug_1 = __importDefault(require("debug")); const storage_1 = require("@google-cloud/storage"); const text_to_speech_1 = require("@google-cloud/text-to-speech"); const constants_js_1 = require("./constants.js"); const dom_js_1 = require("./dom.js"); const schemas_js_1 = require("./schemas.js"); const transforms_js_1 = require("./transforms.js"); const utils_js_1 = require("./utils.js"); const writers_js_1 = require("./writers.js"); var utils_js_2 = require("./utils.js"); Object.defineProperty(exports, "audioExtension", { enumerable: true, get: function () { return utils_js_2.audioExtension; } }); Object.defineProperty(exports, "mediaType", { enumerable: true, get: function () { return utils_js_2.mediaType; } }); const debug = (0, debug_1.default)(`${constants_js_1.DEBUG_PREFIX}/index`); /** * Adds Text-to-Speech functionality to an Eleventy site. */ const configFunction = (eleventyConfig, options) => { const result = schemas_js_1.options.validate(options); if (result.error) { const message = `${constants_js_1.ERROR_MESSAGE_PREFIX.invalidConfiguration}: ${result.error.message}`; throw new Error(message); } let writer; if ('bucketName' in options.audioHost) { const bucketName = options.audioHost.bucketName; const storage = new storage_1.Storage({ keyFilename: (0, utils_js_1.clientLibraryCredentials)({ what: 'Cloud Storage', keyFilename: options.audioHost.keyFilename }) }); writer = (0, writers_js_1.cloudStorageWriter)({ bucketName, storage }); debug(`audio assets will be hosted on Cloud Storage bucket ${bucketName}`); } else { /* eslint-disable @typescript-eslint/no-explicit-any */ const output = eleventyConfig.dir.output; const { origin, pathname } = options.audioHost; const hrefBase = `${origin}${pathname}`; const outputBase = node_path_1.default.join(node_path_1.default.basename(output), pathname); if (!node_fs_1.default.existsSync(outputBase)) { node_fs_1.default.mkdirSync(outputBase, { recursive: true }); } writer = (0, writers_js_1.selfHostWriter)({ hrefBase, outputBase }); debug(`audio assets will be hosted at ${hrefBase} (self-hosted at ${outputBase})`); } const { audioEncodings, cacheExpiration, collectionName, rules, transformName, voice } = result.value; const audioInnerHTML = options.audioInnerHTML ? options.audioInnerHTML : dom_js_1.defaultAudioInnerHTML; /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ const cfg = eleventyConfig; const addCollection = cfg.addCollection.bind(eleventyConfig); const textToSpeechClient = new text_to_speech_1.TextToSpeechClient({ keyFilename: (0, utils_js_1.clientLibraryCredentials)({ what: 'Text-to-Speech client library', keyFilename: result.value.keyFilename }) }); /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ const cb = function templatesWithAudio(collectionApi) { /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ const templates = collectionApi.getAll().filter((item) => { const idx = rules.findIndex((rule) => rule.regex.test(item.outputPath)); return idx !== -1; }); return templates; }; addCollection(collectionName, cb); debug(`11ty collection added: ${collectionName}`); const injectAudioTagsIntoHtml = (0, transforms_js_1.makeInjectAudioTagsIntoHtml)({ audioEncodings, audioInnerHTML, cacheExpiration, rules, textToSpeechClient, transformName, voice, writer }); eleventyConfig.addTransform(transformName, injectAudioTagsIntoHtml); }; exports.plugin = { initArguments: {}, configFunction }; //# sourceMappingURL=index.js.map