UNPKG

@hyperaudio/transcript-renderer

Version:
2 lines (1 loc) 1.57 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.transcriptRenderer=t()}(this,function(){"use strict";return function(e,t,a){var r=arguments.length>3&&void 0!==arguments[3]?arguments[3]:"T",d=arguments.length>4&&void 0!==arguments[4]&&arguments[4],s=arguments.length>5&&void 0!==arguments[5]?arguments[5]:2,n=r.startsWith("T")?1:1e3,i=t.createElement("article");return a.appendChild(i),e.paragraphs.forEach(function(a){var p=t.createElement("p");if(d&&a.speaker){var o=t.createElement(r.startsWith("T")?"span":"a");o.appendChild(t.createTextNode(a.speaker)),o.setAttribute("class","speaker"),"M"===r&&o.setAttribute("data-m",parseFloat((a.start*n).toFixed(s))),p.appendChild(o),p.appendChild(t.createTextNode(" "))}e.words.forEach(function(e,i,o){if(e.start&&!(d&&e.speaker||e.start<a.start||e.start>=a.end)){var l=t.createElement(r.startsWith("T")?"span":"a");l.appendChild(t.createTextNode(e.text+" "));var c=i>0?o[i-1].end||o[i-1].start:0,u=parseFloat((n*(e.start-c)).toFixed(s)),f=parseFloat((e.start*n).toFixed(s)),h=parseFloat((e.end?e.end*n:e.start).toFixed(s)),x=parseFloat((n*(e.end-e.start)).toFixed(s));"T"===r?l.setAttribute("data-t",(f+","+h).replace(/^0/,"").replace(/,0/,",")):"T+"===r?l.setAttribute("data-t",(f+"+"+x).replace(/^0/,"").replace(/\+0/,"+")):"T~"===r?l.setAttribute("data-t",(u+"~"+x).replace(/^0/,"").replace(/^-0/,"-").replace(/~0/,"~")):"M"===r&&(l.setAttribute("data-m",f),e.end&&l.setAttribute("data-d",x)),p.appendChild(l)}}),i.appendChild(p)}),a}});