-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathmdfiles.js
1 lines (1 loc) · 1.75 KB
/
pathmdfiles.js
1
const fs=require("fs"),path=require("path");function fixImagePaths(e){return e.replace(/\.\.\/assets\//g,"https://faq.passkeys.2stable.com/help/assets/")}function removeComments(e){return e.replace(/<!--(.*?)-->/gs,"")}function setTitle(e){const t=e.match(/^(###|##) \*\*(.*?)\*\*/gm);if(t){const e=t[0].match(/\*\*(.*?)\*\*/);if(e)return e[1].trim()}return null}function removeLinesWithPrefix(e,t){const n=new RegExp(`^(${t.join("|")})\\s+\\*\\*.*$`,"gm");return e.replace(n,"")}function removeLeadingEmptyLines(e){return e.replace(/^\s*\n/,"")}function replaceContactLinks(e){return e.replace(/\[([*]+)(Contact Us)([*]+)\]\(passkeys:\/\/contact\?subject\=/g,"[$1$2$3](mailto:support@2stable.com?subject=[Passkeys%20App][Web]%20")}const directoryPath="_help/iOS",questionData=[];fs.readdir("_help/iOS",(e,t)=>{e?console.error("Error reading directory:",e):t.forEach(e=>{const t=path.join("_help/iOS",e);fs.readFile(t,"utf8",(n,r)=>{if(n)return void console.error(`Error reading file ${t}:`,n);const o=removeComments(fixImagePaths(r)),s=setTitle(o);if(s){const n=replaceContactLinks(removeLeadingEmptyLines(removeLinesWithPrefix(o,["###","##"]))),r=path.basename(e,path.extname(e)),i=s.toLowerCase().trim().split(/[\s,\t,\n]+/).join("-").replace(/["#.?]/g,""),a={question:r,permalink:`/help/iOS/${i}/`};questionData.push(a);const l=`---\nlayout: help\ntitle: ${s}\npermalink: /help/iOS/${i}/\nquestion: ${r}\n---\n\n${n}`;fs.writeFile(t,l,e=>{e?console.error(`Error writing to ${t}:`,e):console.log(`Front matter title set for ${t}`),function(e){const t=JSON.stringify(e,null,2);fs.writeFile("_help/iOS/questions.json",t,"utf8",e=>{e?console.error("Error writing JSON file:",e):console.log("JSON file saved successfully.")})}(questionData)})}else console.log(`No title found in ${t}`)})})});