Explorar el Código

+timecodes functions, +defaultMD

Ivan Asmer hace 4 años
padre
commit
c59eb1aa9f
Se han modificado 1 ficheros con 22 adiciones y 1 borrados
  1. 22 1
      index.mjs

+ 22 - 1
index.mjs

@@ -3,6 +3,27 @@
  */
 
 
+const getSecondsFromTimeCode = timeCode => {
+    let times = timeCode.split(':').map(x => +x).filter(x => !isNaN(x))
+    return  times.length === 2 ? times[0]*60 + times[1] : 
+                    (times.length === 3 ? times[0]*60*60 + times[1]*60 + times[2] : NaN )
+}
+
+const getTimeCodes = ast =>{
+    let timeCodes = {}
+    const walker = node => ((node.tag && node.tag.startsWith('heading') && (timeCodes[node.startMatch[4] || node.startMatch[3]] = node.startMatch[3])), 
+                            (node.children instanceof Array && node.children.forEach(walker)))
+    walker(ast)
+    delete timeCodes.undefined
+
+    let result = {}
+    for (let timeCode in timeCodes){
+        result[getSecondsFromTimeCode(timeCode)] = timeCodes[timeCode]
+    }
+    return result;
+}
+
+
 const syntax = {
     img:{
         paired: true,
@@ -681,4 +702,4 @@ function toReact(ast, React, mapMDToComponents=defaultMapMDToComponents){
     return _(RenderComponent, props)
 }
 
-export {buildAST, toReact}
+export {buildAST, toReact, getSecondsFromTimeCode, getTimeCodes, defaultMapMDToComponents}