UNPKG

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

Version:
118 lines 4.93 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeInjectAudioTagsIntoHtml = void 0; const debug_1 = __importDefault(require("debug")); const jsdom_1 = require("jsdom"); const constants_js_1 = require("./constants.js"); const dom_js_1 = require("./dom.js"); const audio_assets_from_text_js_1 = require("./audio-assets-from-text.js"); const debug = (0, debug_1.default)(`${constants_js_1.DEBUG_PREFIX}/transforms`); const injectIntoDom = async ({ audioEncodings, audioInnerHTML, cacheExpiration, dom, outputPath, rule, textToSpeechClient, voice, writer }) => { const doc = dom.window.document; const { regex, cssSelectors, xPathExpressions } = rule; debug(`${outputPath} matches regex pattern "${regex.toString()}", so it will be transformed`); const cssMatches = cssSelectors.map((selector) => { return { selector, texts: (0, dom_js_1.cssSelectorMatchesToTexts)({ dom, selector }) }; }); debug(`found ${cssMatches.length} CSS selector match/es on ${outputPath} (document title: ${doc.title})`); const cssPromises = cssMatches.map(async (cssMatch) => { const assetsPromises = cssMatch.texts.map((text) => (0, audio_assets_from_text_js_1.audioAssetsFromText)({ audioEncodings, cacheExpiration, outputPath, text, textToSpeechClient, voice, writer })); const results = await Promise.all(assetsPromises); return { selector: cssMatch.selector, results }; }); const cssResults = await Promise.all(cssPromises); cssResults.forEach((cssResult) => { const hrefs = cssResult.results.map((res) => res.hrefs); (0, dom_js_1.insertAudioPlayersMatchingCssSelector)({ audioInnerHTML, cssSelector: cssResult.selector, dom, hrefs }); }); debug(`${cssResults.length} audio tags injected because of CSS selector matches`); const xPathMatches = xPathExpressions.map((expression) => { return { expression, texts: (0, dom_js_1.xPathExpressionMatchesToTexts)({ dom, expression }) }; }); debug(`found ${xPathMatches.length} XPath expression match/es on ${outputPath} (document title: ${doc.title})`); const xPathPromises = xPathMatches.map(async (xPathMatch) => { const assetsPromises = xPathMatch.texts.map((text) => (0, audio_assets_from_text_js_1.audioAssetsFromText)({ audioEncodings, cacheExpiration, outputPath, text, textToSpeechClient, voice, writer })); const results = await Promise.all(assetsPromises); return { expression: xPathMatch.expression, results }; }); const xPathResults = await Promise.all(xPathPromises); xPathResults.forEach((xPathResult) => { const hrefs = xPathResult.results.map((res) => res.hrefs); (0, dom_js_1.insertAudioPlayersMatchingXPathExpression)({ audioInnerHTML, dom, expression: xPathResult.expression, hrefs }); }); debug(`${xPathResults.length} audio tags injected because of XPath expression matches`); }; const makeInjectAudioTagsIntoHtml = (config) => { const { audioEncodings, audioInnerHTML, cacheExpiration, rules, textToSpeechClient, transformName, voice, writer } = config; return async function injectAudioTagsIntoHtml(content, outputPath) { debug(`transform ${transformName} invoked on ${outputPath}`); const indexes = rules .map((m, i) => { return m.regex.test(outputPath) ? { i, matched: true } : { i, matched: false }; }) .filter((d) => d.matched) .map((d) => d.i); if (indexes.length === 0) { debug(`${outputPath} does NOT match any regex pattern, so the transform ${transformName} will NOT transform it`); return content; } else { debug(`${outputPath} matches ${indexes.length} regex pattern/s`); } const dom = new jsdom_1.JSDOM(content); const promises = indexes.map((i) => { return injectIntoDom({ audioEncodings, audioInnerHTML, cacheExpiration, dom, outputPath, rule: rules[i], textToSpeechClient, voice, writer }); }); await Promise.all(promises); return dom.serialize(); }; }; exports.makeInjectAudioTagsIntoHtml = makeInjectAudioTagsIntoHtml; //# sourceMappingURL=transforms.js.map