UNPKG

md2poui

Version:

Conversor de arquivos markdown para páginas Angular com suporte ao PO-UI

1 lines 5.55 kB
"use strict";var __createBinding=this&&this.__createBinding||(Object.create?function(e,t,s,r){void 0===r&&(r=s);var o=Object.getOwnPropertyDescriptor(t,s);o&&!("get"in o?!t.__esModule:o.writable||o.configurable)||(o={enumerable:!0,get:function(){return t[s]}}),Object.defineProperty(e,r,o)}:function(e,t,s,r){void 0===r&&(r=s),e[r]=t[s]}),__setModuleDefault=this&&this.__setModuleDefault||(Object.create?function(e,t){Object.defineProperty(e,"default",{enumerable:!0,value:t})}:function(e,t){e.default=t}),__importStar=this&&this.__importStar||function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var s in e)"default"!==s&&Object.prototype.hasOwnProperty.call(e,s)&&__createBinding(t,e,s);return __setModuleDefault(t,e),t},__importDefault=this&&this.__importDefault||function(e){return e&&e.__esModule?e:{default:e}};Object.defineProperty(exports,"__esModule",{value:!0}),exports.Converter=void 0;const fs=__importStar(require("fs")),marked_1=__importDefault(require("marked")),mustache_1=__importDefault(require("mustache")),path=__importStar(require("path")),component_1=require("./component"),helpers_1=require("./helpers"),renderer_1=require("./renderer"),templates=__importStar(require("./templates"));class Converter{srcPath;destDir;options;constructor(e,t,s){this.srcPath=e,this.destDir=t,this.options=s}execute(){this.options.home&&this.createHomeFile();const e=this.getMarkdownFiles().map(((e,t,s)=>new component_1.Component(this.srcPath,e,this.options,t===s.length-1?"":",")));e.forEach((e=>this.createComponentFiles(e))),this.options.createHelpers&&(this.createModuleFile(e),this.createRouterFile(e),this.createServiceFile(e))}convert(e){e=helpers_1.Transform.textToIcon(e);const t=new renderer_1.PoRenderer,s=marked_1.default.marked(e,{renderer:t});return mustache_1.default.render(templates.componentView(),{title:t.getTitle(),content:s})}createHomeFile(){const e=this.options.moduleName,t=helpers_1.Transform.pascalCase(e);this.createComponentDirectory(".");const s=mustache_1.default.render(templates.home(),{moduleClassName:t,moduleName:e});this.writeFile(path.join(this.destDir,`${this.options.moduleName}-home.component.ts`),s);const r=mustache_1.default.render(templates.homeView(),{moduleClassName:t,moduleName:e});this.writeFile(path.join(this.destDir,`${this.options.moduleName}-home.component.html`),r)}createComponentFiles(e){const t=this.createComponentDirectory(e.getPath()),s=this.renderComponentClass(e);this.writeFile(path.join(t,`${e.getName()}.component.ts`),s);const r=this.renderComponentView(e);this.writeFile(path.join(t,`${e.getName()}.component.html`),r)}createComponentDirectory(e){const t=path.join(this.destDir,e);return fs.existsSync(t)||fs.mkdirSync(t,{recursive:!0}),t}renderComponentClass(e){return mustache_1.default.render(templates.component(),{component:e})}renderComponentView(e){const t=helpers_1.Transform.textToIcon(fs.readFileSync(e.getFile(),"utf-8"));this.options.renderer=new renderer_1.PoRenderer;const s=marked_1.default.marked(t,this.options);return e.setTitle(this.options.renderer.getTitle()),this.options.copyExternalFiles&&this.copyFiles(path.dirname(e.getFile()),path.join(this.destDir,this.options.resourceFolderName),this.options.renderer.getFiles()),mustache_1.default.render(templates.componentView(),{title:e.getTitle(),content:s})}createModuleFile(e){const t=this.options.imports,s=this.options.home,r=this.options.moduleName,o=helpers_1.Transform.pascalCase(r),i=mustache_1.default.render(templates.templateModule(),{imports:t,home:s,components:e,moduleName:r,moduleClassName:o});this.writeFile(path.join(this.destDir,`${this.options.moduleName}.module.ts`),i)}createRouterFile(e){const t=this.options.home,s=this.options.moduleName,r=helpers_1.Transform.pascalCase(s),o=mustache_1.default.render(templates.routing(),{home:t,components:e,moduleName:s,moduleClassName:r});this.writeFile(path.join(this.destDir,`${this.options.moduleName}-routing.module.ts`),o)}createServiceFile(e){const t=this.loadMenuItems(e),s=this.options.moduleName,r=helpers_1.Transform.pascalCase(s),o=mustache_1.default.render(templates.service(),{moduleClassName:r,menuItems:t},{menuItem:templates.menuItem()});this.writeFile(path.join(this.destDir,`${this.options.moduleName}.service.ts`),o)}loadMenuItems(e){const t=[];return e.forEach((e=>{const s={label:e.getTitle()||e.getClassName(),menuItems:[],file:e.getFile()};this.options.parentRoutePath&&this.options.parentRoutePath.length>0?s.link=`${this.options.parentRoutePath}/${e.getName()}`:s.link=e.getName();const r=this.options.flatDirs?null:this.getMenuParent(t,e);r?r.push(s):t.push(s)})),t}getMenuParent(e,t){let s=null;for(let r=0,o=e.length;r<o;r++){const o=e[r],i=path.dirname(o.file),n=path.relative(i,t.getFile()),a=new RegExp(`\\${path.sep}`,"g");if(-1===n.indexOf("..")&&1===(n.match(a)||[]).length)return o.menuItems;if(o.menuItems.length>0&&(s=this.getMenuParent(o.menuItems,t)),s)break}return s}copyFiles(e,t,s){fs.existsSync(t)||fs.mkdirSync(t,{recursive:!0}),s.forEach((s=>fs.copyFileSync(path.join(e,s.from),path.join(t,s.to))))}getMarkdownFiles(e=this.srcPath){const t=this.options.recursive;return fs.statSync(e).isFile()?[e]:fs.readdirSync(e).sort(((e,t)=>".md"===path.extname(e)||e<t?-1:1)).map((t=>path.join(e,t))).filter((e=>!this.options.exclusions.some((t=>path.dirname(e).startsWith(t))))).flatMap((e=>t&&fs.statSync(e).isDirectory()?this.getMarkdownFiles(e):e)).filter((e=>-1===this.options.exclusions.indexOf(e)&&".md"===path.extname(e)))}writeFile(e,t){fs.writeFileSync(e,t,"utf-8")}}exports.Converter=Converter;