const { cloneDeep } = require('lodash'); exports.translateComments = (text, lang, dict, codeLang) => { const knownComments = Object.keys(dict); const config = { knownComments, dict, lang }; const input = { text, commentCounts: new Map() }; switch (codeLang) { case 'js': case 'jsx': return transMultiline(transInline(input, config), config); case 'html': return transScript(transHTML(transCSS(input, config), config), config); default: return input; } }; exports.translateCommentsInChallenge = (challenge, lang, dict) => { const challClone = cloneDeep(challenge); if (challClone?.challengeFiles) { challClone.challengeFiles.forEach(challengeFile => { if (challengeFile.contents) { let { text, commentCounts } = this.translateComments( challengeFile.contents, lang, dict, challengeFile.ext ); challClone.__commentCounts = commentCounts; challengeFile.contents = text; } }); } return challClone; }; // bare urls could be interpreted as comments, so we have to lookbehind for // http:// or https:// function transInline(input, config) { return translateGeneric(input, config, '((?.*?<\/style>/gms; const matches = text.matchAll(regex); for (const [match] of matches) { let { text: styleText } = transMultiline( { text: match, commentCounts }, config ); text = text.replace(match, styleText); } return { text, commentCounts }; } function transScript({ text, commentCounts }, config) { const regex = /