comparison src/site.js @ 12:2d4b3f003ec2

tts
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 24 Jul 2025 22:14:49 -0600
parents 46097e607701
children f5425a3c1898
comparison
equal deleted inserted replaced
11:003a90ce72d7 12:2d4b3f003ec2
117 117
118 function scrollBack() { 118 function scrollBack() {
119 window.scrollTo( 0, lastY ); 119 window.scrollTo( 0, lastY );
120 } 120 }
121 121
122
122 // requires markdown-it 123 // requires markdown-it
124
125 let mdDiv = document.createElement('div');
126
123 function handleMarkdown() { 127 function handleMarkdown() {
124 let converter = window.markdownit({html: true}); 128 let converter = window.markdownit({html: true});
125 let divs = document.querySelectorAll('[markdown]'); 129 let divs = document.querySelectorAll('[markdown]');
126 for( let div of divs ) { 130 for( let div of divs ) {
127 let text = div.textContent; 131 let text = div.textContent;
128 text = text.replace(/\{([^|}]+)\|([^|}]+)\}/g, '<ruby>$1<rt>$2</rt></ruby>'); 132 text = text.replace(/\{([^|}]+)\|([^|}]+)\}/g, '<ruby>$1<rt>$2</rt></ruby>');
129 text = converter.render(text); 133 text = converter.render(text);
134 if( div.getAttribute('role')==='assistant' ) {
135 mdDiv.innerHTML = text;
136 let rts = mdDiv.querySelectorAll('rt');
137 for( let rt of rts ) {
138 rt.remove();
139 }
140 //console.log(mdDiv.textContent);
141 text += `\n<p><audio controls preload=none src="/tts.mp3?text=${encodeURIComponent(mdDiv.textContent)}"></p>\n`;
142 }
130 div.innerHTML = text; 143 div.innerHTML = text;
131 div.removeAttribute('markdown'); 144 div.removeAttribute('markdown');
132 } 145 }
133 } 146 }