bajo-template 2.8.0 → 2.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/BajoTemplate.html +1 -1
- package/docs/data/search.json +1 -1
- package/docs/global.html +1 -1
- package/docs/index.html +1 -1
- package/docs/index.js.html +357 -98
- package/docs/lib_imports.js.html +173 -0
- package/docs/module-Imports.html +6 -0
- package/docs/scripts/core.js +477 -476
- package/docs/scripts/resize.js +36 -36
- package/docs/scripts/search.js +105 -105
- package/docs/scripts/third-party/fuse.js +1 -1
- package/docs/scripts/third-party/hljs-line-num-original.js +285 -282
- package/docs/scripts/third-party/hljs-line-num.js +1 -1
- package/docs/scripts/third-party/hljs-original.js +1202 -1195
- package/docs/scripts/third-party/hljs.js +1 -1
- package/docs/scripts/third-party/popper.js +1 -1
- package/docs/scripts/third-party/tippy.js +1 -1
- package/docs/scripts/third-party/tocbot.js +509 -508
- package/index.js +310 -30
- package/lib/imports.js +170 -0
- package/package.json +4 -2
- package/test/bajo-template.test.js +275 -0
- package/wiki/CHANGES.md +8 -0
- package/lib/resolve-resource.js +0 -90
package/docs/index.js.html
CHANGED
|
@@ -1,50 +1,79 @@
|
|
|
1
1
|
<!DOCTYPE html><html lang="en" style="font-size:16px"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Source: index.js</title><!--[if lt IE 9]>
|
|
2
2
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
3
|
-
<![endif]--><script src="scripts/third-party/hljs.js" defer="defer"></script><script src="scripts/third-party/hljs-line-num.js" defer="defer"></script><script src="scripts/third-party/popper.js" defer="defer"></script><script src="scripts/third-party/tippy.js" defer="defer"></script><script src="scripts/third-party/tocbot.min.js"></script><script>var baseURL="/",locationPathname="";baseURL=(locationPathname=document.location.pathname).substr(0,locationPathname.lastIndexOf("/")+1)</script><link rel="stylesheet" href="styles/clean-jsdoc-theme.min.css"><svg aria-hidden="true" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="display:none"><defs><symbol id="copy-icon" viewbox="0 0 488.3 488.3"><g><path d="M314.25,85.4h-227c-21.3,0-38.6,17.3-38.6,38.6v325.7c0,21.3,17.3,38.6,38.6,38.6h227c21.3,0,38.6-17.3,38.6-38.6V124 C352.75,102.7,335.45,85.4,314.25,85.4z M325.75,449.6c0,6.4-5.2,11.6-11.6,11.6h-227c-6.4,0-11.6-5.2-11.6-11.6V124 c0-6.4,5.2-11.6,11.6-11.6h227c6.4,0,11.6,5.2,11.6,11.6V449.6z"/><path d="M401.05,0h-227c-21.3,0-38.6,17.3-38.6,38.6c0,7.5,6,13.5,13.5,13.5s13.5-6,13.5-13.5c0-6.4,5.2-11.6,11.6-11.6h227 c6.4,0,11.6,5.2,11.6,11.6v325.7c0,6.4-5.2,11.6-11.6,11.6c-7.5,0-13.5,6-13.5,13.5s6,13.5,13.5,13.5c21.3,0,38.6-17.3,38.6-38.6 V38.6C439.65,17.3,422.35,0,401.05,0z"/></g></symbol><symbol id="search-icon" viewBox="0 0 512 512"><g><g><path d="M225.474,0C101.151,0,0,101.151,0,225.474c0,124.33,101.151,225.474,225.474,225.474 c124.33,0,225.474-101.144,225.474-225.474C450.948,101.151,349.804,0,225.474,0z M225.474,409.323 c-101.373,0-183.848-82.475-183.848-183.848S124.101,41.626,225.474,41.626s183.848,82.475,183.848,183.848 S326.847,409.323,225.474,409.323z"/></g></g><g><g><path d="M505.902,476.472L386.574,357.144c-8.131-8.131-21.299-8.131-29.43,0c-8.131,8.124-8.131,21.306,0,29.43l119.328,119.328 c4.065,4.065,9.387,6.098,14.715,6.098c5.321,0,10.649-2.033,14.715-6.098C514.033,497.778,514.033,484.596,505.902,476.472z"/></g></g></symbol><symbol id="font-size-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M11.246 15H4.754l-2 5H.6L7 4h2l6.4 16h-2.154l-2-5zm-.8-2L8 6.885 5.554 13h4.892zM21 12.535V12h2v8h-2v-.535a4 4 0 1 1 0-6.93zM19 18a2 2 0 1 0 0-4 2 2 0 0 0 0 4z"/></symbol><symbol id="add-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M11 11V5h2v6h6v2h-6v6h-2v-6H5v-2z"/></symbol><symbol id="minus-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M5 11h14v2H5z"/></symbol><symbol id="dark-theme-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M10 7a7 7 0 0 0 12 4.9v.1c0 5.523-4.477 10-10 10S2 17.523 2 12 6.477 2 12 2h.1A6.979 6.979 0 0 0 10 7zm-6 5a8 8 0 0 0 15.062 3.762A9 9 0 0 1 8.238 4.938 7.999 7.999 0 0 0 4 12z"/></symbol><symbol id="light-theme-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 18a6 6 0 1 1 0-12 6 6 0 0 1 0 12zm0-2a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM11 1h2v3h-2V1zm0 19h2v3h-2v-3zM3.515 4.929l1.414-1.414L7.05 5.636 5.636 7.05 3.515 4.93zM16.95 18.364l1.414-1.414 2.121 2.121-1.414 1.414-2.121-2.121zm2.121-14.85l1.414 1.415-2.121 2.121-1.414-1.414 2.121-2.121zM5.636 16.95l1.414 1.414-2.121 2.121-1.414-1.414 2.121-2.121zM23 11v2h-3v-2h3zM4 11v2H1v-2h3z"/></symbol><symbol id="reset-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M18.537 19.567A9.961 9.961 0 0 1 12 22C6.477 22 2 17.523 2 12S6.477 2 12 2s10 4.477 10 10c0 2.136-.67 4.116-1.81 5.74L17 12h3a8 8 0 1 0-2.46 5.772l.997 1.795z"/></symbol><symbol id="down-icon" viewBox="0 0 16 16"><path fill-rule="evenodd" clip-rule="evenodd" d="M12.7803 6.21967C13.0732 6.51256 13.0732 6.98744 12.7803 7.28033L8.53033 11.5303C8.23744 11.8232 7.76256 11.8232 7.46967 11.5303L3.21967 7.28033C2.92678 6.98744 2.92678 6.51256 3.21967 6.21967C3.51256 5.92678 3.98744 5.92678 4.28033 6.21967L8 9.93934L11.7197 6.21967C12.0126 5.92678 12.4874 5.92678 12.7803 6.21967Z"></path></symbol><symbol id="codepen-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M16.5 13.202L13 15.535v3.596L19.197 15 16.5 13.202zM14.697 12L12 10.202 9.303 12 12 13.798 14.697 12zM20 10.869L18.303 12 20 13.131V10.87zM19.197 9L13 4.869v3.596l3.5 2.333L19.197 9zM7.5 10.798L11 8.465V4.869L4.803 9 7.5 10.798zM4.803 15L11 19.131v-3.596l-3.5-2.333L4.803 15zM4 13.131L5.697 12 4 10.869v2.262zM2 9a1 1 0 0 1 .445-.832l9-6a1 1 0 0 1 1.11 0l9 6A1 1 0 0 1 22 9v6a1 1 0 0 1-.445.832l-9 6a1 1 0 0 1-1.11 0l-9-6A1 1 0 0 1 2 15V9z"/></symbol><symbol id="close-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 10.586l4.95-4.95 1.414 1.414-4.95 4.95 4.95 4.95-1.414 1.414-4.95-4.95-4.95 4.95-1.414-1.414 4.95-4.95-4.95-4.95L7.05 5.636z"/></symbol><symbol id="menu-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M3 4h18v2H3V4zm0 7h18v2H3v-2zm0 7h18v2H3v-2z"/></symbol></defs></svg></head><body data-theme="light"><div class="sidebar-container"><div class="sidebar" id="sidebar"><a href="/" class="sidebar-title sidebar-title-anchor">BajoTemplate API</a><div class="sidebar-items-container"><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-classes"><div>Classes</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="BajoTemplate.html">BajoTemplate</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-global"><div>Global</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="global.html#factory">factory</a></div></div></div></div></div><div class="navbar-container" id="VuAckcnZhf"><nav class="navbar"><div class="navbar-left-items"><div class="navbar-item"><a id="" href="https://www.npmjs.com/package/bajo-template" target="">NPM</a></div><div class="navbar-item"><a id="" href="https://github.com/ardhi/bajo-template" target="">Github</a></div><div class="navbar-item"><a id="" href="https://bajo.app" target="">Bajo</a></div></div><div class="navbar-right-items"><div class="navbar-right-item"><button class="icon-button search-button" aria-label="open-search"><svg><use xlink:href="#search-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button theme-toggle" aria-label="toggle-theme"><svg><use class="theme-svg-use" xlink:href="#dark-theme-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button font-size" aria-label="change-font-size"><svg><use xlink:href="#font-size-icon"></use></svg></button></div></div><nav></nav></nav></div><div class="toc-container"><div class="toc-content"><span class="bold">On this page</span><div id="eed4d2a0bfd64539bb9df78095dec881"></div></div></div><div class="body-wrapper"><div class="main-content"><div class="main-wrapper"><section id="source-page" class="source-page"><header><h1 id="title" class="has-anchor">index.js</h1></header><article><pre class="prettyprint source lang-js"><code>import
|
|
4
|
-
import
|
|
5
|
-
|
|
3
|
+
<![endif]--><script src="scripts/third-party/hljs.js" defer="defer"></script><script src="scripts/third-party/hljs-line-num.js" defer="defer"></script><script src="scripts/third-party/popper.js" defer="defer"></script><script src="scripts/third-party/tippy.js" defer="defer"></script><script src="scripts/third-party/tocbot.min.js"></script><script>var baseURL="/",locationPathname="";baseURL=(locationPathname=document.location.pathname).substr(0,locationPathname.lastIndexOf("/")+1)</script><link rel="stylesheet" href="styles/clean-jsdoc-theme.min.css"><svg aria-hidden="true" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="display:none"><defs><symbol id="copy-icon" viewbox="0 0 488.3 488.3"><g><path d="M314.25,85.4h-227c-21.3,0-38.6,17.3-38.6,38.6v325.7c0,21.3,17.3,38.6,38.6,38.6h227c21.3,0,38.6-17.3,38.6-38.6V124 C352.75,102.7,335.45,85.4,314.25,85.4z M325.75,449.6c0,6.4-5.2,11.6-11.6,11.6h-227c-6.4,0-11.6-5.2-11.6-11.6V124 c0-6.4,5.2-11.6,11.6-11.6h227c6.4,0,11.6,5.2,11.6,11.6V449.6z"/><path d="M401.05,0h-227c-21.3,0-38.6,17.3-38.6,38.6c0,7.5,6,13.5,13.5,13.5s13.5-6,13.5-13.5c0-6.4,5.2-11.6,11.6-11.6h227 c6.4,0,11.6,5.2,11.6,11.6v325.7c0,6.4-5.2,11.6-11.6,11.6c-7.5,0-13.5,6-13.5,13.5s6,13.5,13.5,13.5c21.3,0,38.6-17.3,38.6-38.6 V38.6C439.65,17.3,422.35,0,401.05,0z"/></g></symbol><symbol id="search-icon" viewBox="0 0 512 512"><g><g><path d="M225.474,0C101.151,0,0,101.151,0,225.474c0,124.33,101.151,225.474,225.474,225.474 c124.33,0,225.474-101.144,225.474-225.474C450.948,101.151,349.804,0,225.474,0z M225.474,409.323 c-101.373,0-183.848-82.475-183.848-183.848S124.101,41.626,225.474,41.626s183.848,82.475,183.848,183.848 S326.847,409.323,225.474,409.323z"/></g></g><g><g><path d="M505.902,476.472L386.574,357.144c-8.131-8.131-21.299-8.131-29.43,0c-8.131,8.124-8.131,21.306,0,29.43l119.328,119.328 c4.065,4.065,9.387,6.098,14.715,6.098c5.321,0,10.649-2.033,14.715-6.098C514.033,497.778,514.033,484.596,505.902,476.472z"/></g></g></symbol><symbol id="font-size-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M11.246 15H4.754l-2 5H.6L7 4h2l6.4 16h-2.154l-2-5zm-.8-2L8 6.885 5.554 13h4.892zM21 12.535V12h2v8h-2v-.535a4 4 0 1 1 0-6.93zM19 18a2 2 0 1 0 0-4 2 2 0 0 0 0 4z"/></symbol><symbol id="add-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M11 11V5h2v6h6v2h-6v6h-2v-6H5v-2z"/></symbol><symbol id="minus-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M5 11h14v2H5z"/></symbol><symbol id="dark-theme-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M10 7a7 7 0 0 0 12 4.9v.1c0 5.523-4.477 10-10 10S2 17.523 2 12 6.477 2 12 2h.1A6.979 6.979 0 0 0 10 7zm-6 5a8 8 0 0 0 15.062 3.762A9 9 0 0 1 8.238 4.938 7.999 7.999 0 0 0 4 12z"/></symbol><symbol id="light-theme-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 18a6 6 0 1 1 0-12 6 6 0 0 1 0 12zm0-2a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM11 1h2v3h-2V1zm0 19h2v3h-2v-3zM3.515 4.929l1.414-1.414L7.05 5.636 5.636 7.05 3.515 4.93zM16.95 18.364l1.414-1.414 2.121 2.121-1.414 1.414-2.121-2.121zm2.121-14.85l1.414 1.415-2.121 2.121-1.414-1.414 2.121-2.121zM5.636 16.95l1.414 1.414-2.121 2.121-1.414-1.414 2.121-2.121zM23 11v2h-3v-2h3zM4 11v2H1v-2h3z"/></symbol><symbol id="reset-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M18.537 19.567A9.961 9.961 0 0 1 12 22C6.477 22 2 17.523 2 12S6.477 2 12 2s10 4.477 10 10c0 2.136-.67 4.116-1.81 5.74L17 12h3a8 8 0 1 0-2.46 5.772l.997 1.795z"/></symbol><symbol id="down-icon" viewBox="0 0 16 16"><path fill-rule="evenodd" clip-rule="evenodd" d="M12.7803 6.21967C13.0732 6.51256 13.0732 6.98744 12.7803 7.28033L8.53033 11.5303C8.23744 11.8232 7.76256 11.8232 7.46967 11.5303L3.21967 7.28033C2.92678 6.98744 2.92678 6.51256 3.21967 6.21967C3.51256 5.92678 3.98744 5.92678 4.28033 6.21967L8 9.93934L11.7197 6.21967C12.0126 5.92678 12.4874 5.92678 12.7803 6.21967Z"></path></symbol><symbol id="codepen-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M16.5 13.202L13 15.535v3.596L19.197 15 16.5 13.202zM14.697 12L12 10.202 9.303 12 12 13.798 14.697 12zM20 10.869L18.303 12 20 13.131V10.87zM19.197 9L13 4.869v3.596l3.5 2.333L19.197 9zM7.5 10.798L11 8.465V4.869L4.803 9 7.5 10.798zM4.803 15L11 19.131v-3.596l-3.5-2.333L4.803 15zM4 13.131L5.697 12 4 10.869v2.262zM2 9a1 1 0 0 1 .445-.832l9-6a1 1 0 0 1 1.11 0l9 6A1 1 0 0 1 22 9v6a1 1 0 0 1-.445.832l-9 6a1 1 0 0 1-1.11 0l-9-6A1 1 0 0 1 2 15V9z"/></symbol><symbol id="close-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M12 10.586l4.95-4.95 1.414 1.414-4.95 4.95 4.95 4.95-1.414 1.414-4.95-4.95-4.95 4.95-1.414-1.414 4.95-4.95-4.95-4.95L7.05 5.636z"/></symbol><symbol id="menu-icon" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0z"/><path d="M3 4h18v2H3V4zm0 7h18v2H3v-2zm0 7h18v2H3v-2z"/></symbol></defs></svg></head><body data-theme="light"><div class="sidebar-container"><div class="sidebar" id="sidebar"><a href="/" class="sidebar-title sidebar-title-anchor">BajoTemplate API</a><div class="sidebar-items-container"><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-classes"><div>Classes</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="BajoTemplate.html">BajoTemplate</a></div><div class="sidebar-section-children"><a href="BajoTemplate.html">BajoTemplate</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-modules"><div>Modules</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="module-Imports.html">Imports</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-global"><div>Global</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="global.html#TImportsResult">TImportsResult</a></div><div class="sidebar-section-children"><a href="global.html#factory">factory</a></div></div></div></div></div><div class="navbar-container" id="VuAckcnZhf"><nav class="navbar"><div class="navbar-left-items"><div class="navbar-item"><a id="" href="https://www.npmjs.com/package/bajo-template" target="">NPM</a></div><div class="navbar-item"><a id="" href="https://github.com/ardhi/bajo-template" target="">Github</a></div><div class="navbar-item"><a id="" href="https://bajo.app" target="">Bajo</a></div></div><div class="navbar-right-items"><div class="navbar-right-item"><button class="icon-button search-button" aria-label="open-search"><svg><use xlink:href="#search-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button theme-toggle" aria-label="toggle-theme"><svg><use class="theme-svg-use" xlink:href="#dark-theme-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button font-size" aria-label="change-font-size"><svg><use xlink:href="#font-size-icon"></use></svg></button></div></div><nav></nav></nav></div><div class="toc-container"><div class="toc-content"><span class="bold">On this page</span><div id="eed4d2a0bfd64539bb9df78095dec881"></div></div></div><div class="body-wrapper"><div class="main-content"><div class="main-wrapper"><section id="source-page" class="source-page"><header><h1 id="title" class="has-anchor">index.js</h1></header><article><pre class="prettyprint source lang-js"><code>import crypto from 'crypto'
|
|
4
|
+
import _path from 'path'
|
|
5
|
+
|
|
6
|
+
const cache = {}
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
|
-
* Plugin factory
|
|
9
|
+
* Plugin factory.
|
|
10
|
+
*
|
|
11
|
+
* **Never** call this function directly!!! It's only-meant to be called by the {@link https://ardhi.github.io/bajo|Bajo framework} during plugin initialization.
|
|
9
12
|
*
|
|
13
|
+
* @async
|
|
10
14
|
* @param {string} pkgName - NPM package name
|
|
11
|
-
* @returns {class}
|
|
15
|
+
* @returns {Promise<class>} Promise that resolves to the BajoTemplate class
|
|
12
16
|
*/
|
|
13
17
|
async function factory (pkgName) {
|
|
14
18
|
const me = this
|
|
15
19
|
|
|
16
20
|
/**
|
|
17
|
-
* BajoTemplate class
|
|
21
|
+
* BajoTemplate class definition.
|
|
18
22
|
*
|
|
19
|
-
* @class
|
|
23
|
+
* @class BajoTemplate
|
|
20
24
|
*/
|
|
21
|
-
class BajoTemplate extends this.app.
|
|
22
|
-
|
|
23
|
-
|
|
25
|
+
class BajoTemplate extends this.app.baseClass.Base {
|
|
26
|
+
/**
|
|
27
|
+
* Constructor
|
|
28
|
+
*/
|
|
24
29
|
constructor () {
|
|
25
30
|
super(pkgName, me.app)
|
|
31
|
+
/**
|
|
32
|
+
* @property {object} config - Configuration object
|
|
33
|
+
* @property {object} [config.layout={}] - Layout configuration
|
|
34
|
+
* @property {boolean} [config.layout.fallback=true] - Whether to fallback to default layout if not found
|
|
35
|
+
* @property {Number|string} [config.loopDetectorDur='1m'] - Duration for loop detection
|
|
36
|
+
* @property {object} [config.log={}] - Logging configuration
|
|
37
|
+
* @property {boolean} [config.log.resolver=false] - Whether to log resolver actions
|
|
38
|
+
* @property {object} [config.cache={}] - Cache configuration
|
|
39
|
+
* @property {Number|string} [config.cache.ttlDur='1s'] - Time-to-live duration for cache
|
|
40
|
+
*/
|
|
26
41
|
this.config = {
|
|
27
42
|
layout: {
|
|
28
43
|
fallback: true
|
|
29
44
|
},
|
|
30
45
|
loopDetectorDur: '1m',
|
|
46
|
+
log: {
|
|
47
|
+
resolver: false
|
|
48
|
+
},
|
|
31
49
|
cache: {
|
|
32
|
-
|
|
50
|
+
ttlDur: '1s'
|
|
33
51
|
}
|
|
34
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* @property {object} loopDetector - Loop detection object
|
|
55
|
+
*/
|
|
35
56
|
this.loopDetector = {}
|
|
36
57
|
}
|
|
37
58
|
|
|
38
|
-
|
|
59
|
+
/**
|
|
60
|
+
* Build compile imports. Used to provide utility functions to templates.
|
|
61
|
+
*
|
|
62
|
+
* @method
|
|
63
|
+
* @param {object} locals - Locals object
|
|
64
|
+
* @param {object} req - Request object
|
|
65
|
+
* @returns {TImportsResult} Imports object
|
|
66
|
+
*/
|
|
67
|
+
buildCompileImports = (locals = {}, req = {}) => {
|
|
39
68
|
const _ = this.app.lib._
|
|
40
69
|
return {
|
|
41
70
|
_,
|
|
42
71
|
_t: (text, ...args) => {
|
|
43
|
-
const params = [...args, { lang }]
|
|
72
|
+
const params = [...args, { lang: req.lang }]
|
|
44
73
|
return this.t(text, ...params)
|
|
45
74
|
},
|
|
46
75
|
_format: (val, type, opts = {}) => {
|
|
47
|
-
opts.lang = opts.lang ?? lang
|
|
76
|
+
opts.lang = opts.lang ?? req.lang
|
|
48
77
|
return this.app.bajo.format(val, type, opts)
|
|
49
78
|
},
|
|
50
79
|
_findRoute: (input) => {
|
|
@@ -67,10 +96,24 @@ async function factory (pkgName) {
|
|
|
67
96
|
},
|
|
68
97
|
_dump: (value, noPre) => {
|
|
69
98
|
return (noPre ? '' : '<pre>') + JSON.stringify(value, null, 2) + (noPre ? '' : '</pre>')
|
|
99
|
+
},
|
|
100
|
+
_getSetting: (key, defValue) => {
|
|
101
|
+
if (!this.app.waibu) return _.get(locals, `schema.view.${key}`, defValue)
|
|
102
|
+
const cfg = this.app.waibu.getSetting(key, { defValue, req })
|
|
103
|
+
const { path } = this.app.bajo.breakNsPath(key)
|
|
104
|
+
const paths = path.replaceAll('/', '.').split('.')
|
|
105
|
+
if (paths[0] === '') paths.shift()
|
|
106
|
+
return _.get(locals, `schema.view.${paths.join('.')}`, cfg)
|
|
70
107
|
}
|
|
71
108
|
}
|
|
72
109
|
}
|
|
73
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Clear loop detector by removing entries that have expired based on the configured duration.
|
|
113
|
+
*
|
|
114
|
+
* @method
|
|
115
|
+
* @private
|
|
116
|
+
*/
|
|
74
117
|
_clearLoopDetector = () => {
|
|
75
118
|
const { omit } = this.app.lib._
|
|
76
119
|
const now = Date.now()
|
|
@@ -82,11 +125,20 @@ async function factory (pkgName) {
|
|
|
82
125
|
this.loopDetector = omit(this.loopDetector, omitted)
|
|
83
126
|
}
|
|
84
127
|
|
|
128
|
+
/**
|
|
129
|
+
* Detect loop in template rendering.
|
|
130
|
+
*
|
|
131
|
+
* @method
|
|
132
|
+
* @private
|
|
133
|
+
* @param {string} tpl - Template name
|
|
134
|
+
* @param {string} file - File path
|
|
135
|
+
* @param {object} opts - Options object
|
|
136
|
+
*/
|
|
85
137
|
_detectLoop = (tpl, file, opts) => {
|
|
86
138
|
const { last } = this.app.lib._
|
|
87
139
|
if (opts.groupId) {
|
|
88
140
|
if (this.loopDetector[opts.groupId]) {
|
|
89
|
-
if (last(this.loopDetector[opts.groupId].file) === file &&
|
|
141
|
+
if (last(this.loopDetector[opts.groupId].file) === file && _path.basename(file)[0] !== '~') {
|
|
90
142
|
throw this.error('loopDetected%s%s', tpl, file)
|
|
91
143
|
}
|
|
92
144
|
this.loopDetector[opts.groupId].file.push(file)
|
|
@@ -99,6 +151,17 @@ async function factory (pkgName) {
|
|
|
99
151
|
}
|
|
100
152
|
}
|
|
101
153
|
|
|
154
|
+
/**
|
|
155
|
+
* Render a template.
|
|
156
|
+
*
|
|
157
|
+
* @async
|
|
158
|
+
* @method
|
|
159
|
+
* @private
|
|
160
|
+
* @param {string} tpl - Template name
|
|
161
|
+
* @param {object} locals - Locals object
|
|
162
|
+
* @param {object} opts - Options object
|
|
163
|
+
* @returns {Promise<string>} Rendered content
|
|
164
|
+
*/
|
|
102
165
|
_render = async (tpl, locals = {}, opts = {}) => {
|
|
103
166
|
this._clearLoopDetector()
|
|
104
167
|
const { trim, isEmpty, last } = this.app.lib._
|
|
@@ -107,7 +170,7 @@ async function factory (pkgName) {
|
|
|
107
170
|
|
|
108
171
|
let resp
|
|
109
172
|
let subNs
|
|
110
|
-
if (
|
|
173
|
+
if (_path.isAbsolute(tpl)) resp = { file: tpl }
|
|
111
174
|
else {
|
|
112
175
|
subNs = breakNsPath(tpl).subNs
|
|
113
176
|
if (subNs === 'template') {
|
|
@@ -123,25 +186,36 @@ async function factory (pkgName) {
|
|
|
123
186
|
let { content, frontMatter } = this.splitContent(fileContent)
|
|
124
187
|
if (isEmpty(content)) {
|
|
125
188
|
const sep = '/waibuMpa/route/'
|
|
126
|
-
if (
|
|
189
|
+
if (_path.isAbsolute(tpl) && tpl.includes(sep)) { // for direct waibuMpa's route
|
|
127
190
|
const parts = tpl.split(sep)
|
|
128
191
|
const ns = last(parts[0].split('/'))
|
|
129
192
|
content = `<!-- include ${ns}.partial:/${parts[1]} -->`
|
|
130
193
|
} else content = '<!-- include ' + tpl.replace('.template', '.partial') + ' -->'
|
|
131
194
|
}
|
|
132
|
-
opts.ext =
|
|
195
|
+
opts.ext = _path.extname(file)
|
|
133
196
|
opts.frontMatter = frontMatter
|
|
134
197
|
opts.partial = opts.partial ?? subNs === 'partial'
|
|
135
198
|
return await this._renderString(content, locals, opts)
|
|
136
199
|
}
|
|
137
200
|
|
|
201
|
+
/**
|
|
202
|
+
* Handle include directives in the content.
|
|
203
|
+
*
|
|
204
|
+
* @async
|
|
205
|
+
* @method
|
|
206
|
+
* @private
|
|
207
|
+
* @param {string} content - Content string
|
|
208
|
+
* @param {object} locals - Locals object
|
|
209
|
+
* @param {object} opts - Options object
|
|
210
|
+
* @returns {Promise<string>} Processed content
|
|
211
|
+
*/
|
|
138
212
|
_handleInclude = async (content, locals = {}, opts = {}) => {
|
|
139
213
|
const { isEmpty, omit, template, merge } = this.app.lib._
|
|
140
214
|
const { extractText } = this.app.lib.aneka
|
|
141
215
|
const { breakNsPath } = this.app.bajo
|
|
142
216
|
const start = '<!-- include '
|
|
143
217
|
const end = ' -->'
|
|
144
|
-
const imports = this.buildCompileImports(opts.
|
|
218
|
+
const imports = this.buildCompileImports(locals, opts.req)
|
|
145
219
|
while (content.includes(start) && content.includes(end)) {
|
|
146
220
|
const { pattern, result: rsc } = extractText(content, start, end)
|
|
147
221
|
if (!isEmpty(rsc)) {
|
|
@@ -171,10 +245,21 @@ async function factory (pkgName) {
|
|
|
171
245
|
return content
|
|
172
246
|
}
|
|
173
247
|
|
|
248
|
+
/**
|
|
249
|
+
* Render a string with the given locals and options.
|
|
250
|
+
*
|
|
251
|
+
* @async
|
|
252
|
+
* @method
|
|
253
|
+
* @private
|
|
254
|
+
* @param {string} content - Content string
|
|
255
|
+
* @param {object} locals - Locals object
|
|
256
|
+
* @param {object} opts - Options object
|
|
257
|
+
* @returns {Promise<string>} Rendered content
|
|
258
|
+
*/
|
|
174
259
|
_renderString = async (content, locals = {}, opts = {}) => {
|
|
175
|
-
const { merge, without, isString, omit, kebabCase } = this.app.lib._
|
|
260
|
+
const { merge, without, isString, omit, kebabCase, get } = this.app.lib._
|
|
176
261
|
if (opts.ext === '.md' && this.app.bajoMarkdown) {
|
|
177
|
-
content = await this.compile(content, locals, {
|
|
262
|
+
content = await this.compile(content, locals, { ttl: -1, req: opts.req }) // markdown can't process template tags, hence preprocess here
|
|
178
263
|
content = this.app.bajoMarkdown.parse(content)
|
|
179
264
|
}
|
|
180
265
|
let layout
|
|
@@ -188,7 +273,7 @@ async function factory (pkgName) {
|
|
|
188
273
|
for (const b of ['scriptBlock', 'styleBlock']) {
|
|
189
274
|
locals.page[b] = pageFm[b] ?? opts[b] ?? (locals.page.ns ? `${locals.page.ns}.partial:/${kebabCase(b)}.html` : `bajoTemplate.partial:/${kebabCase(b)}.html`)
|
|
190
275
|
}
|
|
191
|
-
const ext =
|
|
276
|
+
const ext = _path.extname(layout)
|
|
192
277
|
const { file } = this.resolveLayout(layout, opts)
|
|
193
278
|
let { content: layoutContent, frontMatter: layoutFm } = this.splitContent(file, true)
|
|
194
279
|
layoutFm = await this.parseFrontMatter(layoutFm, opts.lang)
|
|
@@ -207,135 +292,287 @@ async function factory (pkgName) {
|
|
|
207
292
|
}
|
|
208
293
|
if (layoutFm.title && !locals.page.title) locals.page.title = layoutFm.title
|
|
209
294
|
content = layoutContent.replace('<!-- body -->', content)
|
|
210
|
-
const
|
|
211
|
-
const fullTitle =
|
|
212
|
-
locals.page.fullTitle = locals.fullTitle ?? fullTitle
|
|
295
|
+
const usePluginTitle = get(this, 'app.waibuMpa.config.page.usePluginTitle')
|
|
296
|
+
const fullTitle = usePluginTitle ? `${locals.page.title} - ${this.app.waibuMpa.getPluginTitle(locals.page.ns, opts.lang)}` : locals.page.title
|
|
297
|
+
locals.page.fullTitle = locals.page.fullTitle ?? fullTitle
|
|
213
298
|
}
|
|
214
|
-
content = await this.compile(content, locals, {
|
|
299
|
+
content = await this.compile(content, locals, { ttl: this.config.cache.maxAgeDu, req: opts.req })
|
|
215
300
|
return await this._handleInclude(content, locals, opts)
|
|
216
301
|
}
|
|
217
302
|
|
|
303
|
+
/**
|
|
304
|
+
* Find a file based on the given options.
|
|
305
|
+
*
|
|
306
|
+
* @method
|
|
307
|
+
* @private
|
|
308
|
+
* @param {object} options - Options object
|
|
309
|
+
* @param {string} options.type - Type of the file
|
|
310
|
+
* @param {string} options.ns - Namespace
|
|
311
|
+
* @param {string} options.subSubNs - Sub-sub namespace
|
|
312
|
+
* @param {string} options.dir - Directory
|
|
313
|
+
* @param {string} options.base - Base name
|
|
314
|
+
* @param {string[]} options.exts - Array of extensions
|
|
315
|
+
* @param {object} options.theme - Theme object
|
|
316
|
+
* @param {object} options.req - Request object
|
|
317
|
+
* @returns {string|null} - Found file path or null
|
|
318
|
+
*/
|
|
319
|
+
_findFile = (options = {}) => {
|
|
320
|
+
const { type, ns, subSubNs, dir, base, exts, theme, req } = options
|
|
321
|
+
const checking = ({ check, info }) => {
|
|
322
|
+
const { fs } = this.app.lib
|
|
323
|
+
let file
|
|
324
|
+
for (const ext of exts) {
|
|
325
|
+
let path = `${check}/${dir}/${base}${ext}`
|
|
326
|
+
if (['', '.'].includes(dir)) path = `${check}/${base}${ext}`
|
|
327
|
+
if (fs.existsSync(path)) {
|
|
328
|
+
file = path
|
|
329
|
+
break
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
if (this.config.log.resolver) {
|
|
333
|
+
this.log.trace('Checking %s for "%s" -> %s', info, base, check)
|
|
334
|
+
if (file) this.log.trace('OK: %s', file)
|
|
335
|
+
else this.log.trace('Failed')
|
|
336
|
+
}
|
|
337
|
+
return file
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const extension = subSubNs ? `${subSubNs}/` : ''
|
|
341
|
+
let file
|
|
342
|
+
if (this.app.waibu && req) {
|
|
343
|
+
const hostname = this.app.waibu.getHostname(req)
|
|
344
|
+
const check = `${this.app.main.dir.pkg}/extend/${this.ns}/${hostname}/${ns}/${extension}${type}`
|
|
345
|
+
file = checking({ check, info: '1. main override: domain specific' })
|
|
346
|
+
}
|
|
347
|
+
if (theme && !file) {
|
|
348
|
+
const check = `${this.app.main.dir.pkg}/extend/${this.ns}/${theme.name}/${ns}/${extension}${type}`
|
|
349
|
+
file = checking({ check, info: '2. main override: theme specific' })
|
|
350
|
+
}
|
|
351
|
+
if (!file) {
|
|
352
|
+
const check = `${this.app.main.dir.pkg}/extend/${this.ns}/${ns}/${extension}${type}`
|
|
353
|
+
file = checking({ check, info: '3. main override: common' })
|
|
354
|
+
}
|
|
355
|
+
if (theme && !file) {
|
|
356
|
+
const check = `${theme.plugin.dir.pkg}/extend/${this.ns}/${theme.name}/${ns}/${extension}${type}`
|
|
357
|
+
file = checking({ check, info: '4. override: theme specific' })
|
|
358
|
+
}
|
|
359
|
+
if (theme && !file) {
|
|
360
|
+
const check = `${theme.plugin.dir.pkg}/extend/${this.ns}/_common/${ns}/${extension}${type}`
|
|
361
|
+
file = checking({ check, info: '5. override: theme specific (common)' })
|
|
362
|
+
}
|
|
363
|
+
if (theme && !file) {
|
|
364
|
+
const check = `${this.app[ns].dir.pkg}/extend/${this.ns}/${theme.name}/${extension}${type}`
|
|
365
|
+
file = checking({ check, info: '6. real: theme specific' })
|
|
366
|
+
}
|
|
367
|
+
if (!file) {
|
|
368
|
+
const check = `${this.app[ns].dir.pkg}/extend/${this.ns}/${extension}${type}`
|
|
369
|
+
file = checking({ check, info: '7. real: common' })
|
|
370
|
+
}
|
|
371
|
+
return file
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
/**
|
|
375
|
+
* Get a resource by name.
|
|
376
|
+
*
|
|
377
|
+
* @method
|
|
378
|
+
* @param {string} name - Resource name
|
|
379
|
+
* @returns {object} Resource object
|
|
380
|
+
*/
|
|
218
381
|
getResource = (name) => {
|
|
219
382
|
const subNses = ['layout', 'template', 'partial']
|
|
220
383
|
const { ns, path, subNs, subSubNs, qs } = this.app.bajo.breakNsPath(name)
|
|
221
|
-
const plugin = this.app.
|
|
384
|
+
const plugin = this.app.getPlugin(ns)
|
|
222
385
|
const dir = `${plugin.dir.pkg}/extend/bajoTemplate`
|
|
223
386
|
if (!subNses.includes(subNs)) throw this.error('unknownResource%s', name)
|
|
224
387
|
const fullPath = subSubNs ? `${dir}/${subSubNs}/${subNs}${path}` : `${dir}/${subNs}${path}`
|
|
225
388
|
return { ns, subNs, subSubNs, path, qs, fullPath }
|
|
226
389
|
}
|
|
227
390
|
|
|
391
|
+
/**
|
|
392
|
+
* Parse front matter from the input string.
|
|
393
|
+
*
|
|
394
|
+
* @async
|
|
395
|
+
* @method
|
|
396
|
+
* @param {string} input - Input string
|
|
397
|
+
* @param {string} lang - Language
|
|
398
|
+
* @returns {Promise<object>} Parsed front matter
|
|
399
|
+
*/
|
|
228
400
|
parseFrontMatter = async (input = '', lang) => {
|
|
229
|
-
const { isEmpty
|
|
230
|
-
const {
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
try {
|
|
236
|
-
const result = await handler(input, true)
|
|
237
|
-
if (isPlainObject(result) || isArray(result)) success = result
|
|
238
|
-
} catch (err) {
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
if (isEmpty(success)) return {}
|
|
242
|
-
return parseObject(success, { parseValue: true, lang }) ?? {}
|
|
401
|
+
const { isEmpty } = this.app.lib._
|
|
402
|
+
const { readAsConfig } = this.app.bajo
|
|
403
|
+
const { parseObject } = this.app.lib
|
|
404
|
+
const result = await readAsConfig(input)
|
|
405
|
+
if (isEmpty(result)) return {}
|
|
406
|
+
return parseObject(result, { parseValue: false, lang }) ?? {}
|
|
243
407
|
}
|
|
244
408
|
|
|
245
|
-
|
|
409
|
+
/**
|
|
410
|
+
* Compile a template with the given content and locals.
|
|
411
|
+
*
|
|
412
|
+
* @async
|
|
413
|
+
* @method
|
|
414
|
+
* @param {string} content - Template content
|
|
415
|
+
* @param {object} locals - Locals object
|
|
416
|
+
* @param {object} options - Options object
|
|
417
|
+
* @param {number} options.ttl - Time to live for cache
|
|
418
|
+
* @param {object} options.req - Request object
|
|
419
|
+
* @returns {Promise<Function>} Compiled template function
|
|
420
|
+
*/
|
|
421
|
+
compile = async (content, locals, { ttl = 0, req = {} } = {}) => {
|
|
422
|
+
const { get: getCache, set: setCache } = this.app.bajoCache ?? {}
|
|
423
|
+
const { template } = this.app.lib._
|
|
246
424
|
locals.attr = locals.attr ?? {}
|
|
247
|
-
const _ = this.app.lib._
|
|
248
|
-
const { template } = _
|
|
249
|
-
const cache = this.app.bajoCache
|
|
250
|
-
let canCache = this.config.cache !== false && cache && this.app.bajo.config.env !== 'dev'
|
|
251
|
-
if (ttl === -1) canCache = false
|
|
252
425
|
const opts = {
|
|
253
|
-
imports: this.buildCompileImports(
|
|
426
|
+
imports: this.buildCompileImports(locals, req)
|
|
254
427
|
}
|
|
255
|
-
|
|
428
|
+
const key = 'fn:' + crypto.createHash('md5').update(content).digest('hex')
|
|
256
429
|
let item
|
|
257
|
-
if (
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
item = await cache.sync({ key, value, ttl })
|
|
261
|
-
} else {
|
|
262
|
-
item = template(content, opts)
|
|
430
|
+
if (getCache) {
|
|
431
|
+
item = await getCache({ key })
|
|
432
|
+
if (item) return item(locals)
|
|
263
433
|
}
|
|
434
|
+
item = template(content, opts)
|
|
435
|
+
if (setCache) await setCache({ key, value: item, ttl })
|
|
264
436
|
return item(locals)
|
|
265
437
|
}
|
|
266
438
|
|
|
439
|
+
/**
|
|
440
|
+
* Render a string with the given locals and options.
|
|
441
|
+
*
|
|
442
|
+
* @async
|
|
443
|
+
* @method
|
|
444
|
+
* @param {string} content - Content string
|
|
445
|
+
* @param {object} locals - Locals object
|
|
446
|
+
* @param {object} opts - Options object
|
|
447
|
+
* @returns {Promise<string>} Rendered content
|
|
448
|
+
*/
|
|
267
449
|
renderString = async (content, locals = {}, opts = {}) => {
|
|
268
450
|
let text = await this._renderString(content, locals, opts)
|
|
269
451
|
if (opts.postProcessor) text = await opts.postProcessor({ text, locals, opts })
|
|
270
452
|
return text
|
|
271
453
|
}
|
|
272
454
|
|
|
455
|
+
/**
|
|
456
|
+
* Render a template with the given locals and options.
|
|
457
|
+
*
|
|
458
|
+
* @async
|
|
459
|
+
* @method
|
|
460
|
+
* @param {string} tpl - Template name
|
|
461
|
+
* @param {object} locals - Locals object
|
|
462
|
+
* @param {object} opts - Options object
|
|
463
|
+
* @returns {Promise<string>} Rendered template
|
|
464
|
+
*/
|
|
273
465
|
render = async (tpl, locals = {}, opts = {}) => {
|
|
274
466
|
const { runHook, breakNsPath } = this.app.bajo
|
|
275
467
|
const { upperFirst } = this.app.lib._
|
|
276
|
-
const
|
|
468
|
+
const { get, set } = this.app.bajoCache ?? {}
|
|
277
469
|
const key = crypto.createHash('md5').update(`${tpl}:${JSON.stringify(locals)}`).digest('hex')
|
|
278
470
|
let subNs
|
|
279
|
-
const isAbsolute =
|
|
471
|
+
const isAbsolute = _path.isAbsolute(tpl)
|
|
280
472
|
if (!isAbsolute) subNs = breakNsPath(tpl).subNs
|
|
281
|
-
const canCache = (isAbsolute || subNs === 'template') && this.config.cache !== false &&
|
|
473
|
+
const canCache = (isAbsolute || subNs === 'template') && this.config.cache !== false && get && set
|
|
282
474
|
if (canCache) {
|
|
283
|
-
const item = await
|
|
475
|
+
const item = await get({ key })
|
|
284
476
|
if (item) return item
|
|
285
477
|
}
|
|
286
478
|
if (subNs) await runHook(`${this.ns}:beforeRender${upperFirst(subNs)}`, { tpl, locals, opts })
|
|
287
479
|
let text = await this._render(tpl, locals, opts)
|
|
288
480
|
if (opts.postProcessor) text = await opts.postProcessor({ text, locals, opts })
|
|
289
481
|
if (subNs) await runHook(`${this.ns}:afterRender${upperFirst(subNs)}`, { tpl, locals, opts, text })
|
|
290
|
-
if (canCache) await
|
|
482
|
+
if (canCache) await set({ key, value: text, ttl: opts.ttlDur ?? this.config.cache.ttlDur })
|
|
291
483
|
return text
|
|
292
484
|
}
|
|
293
485
|
|
|
486
|
+
/**
|
|
487
|
+
* Resolve a resource by type and name.
|
|
488
|
+
*
|
|
489
|
+
* @method
|
|
490
|
+
* @param {string} type - Resource type
|
|
491
|
+
* @param {string} item - Resource name
|
|
492
|
+
* @param {object} opts - Options object
|
|
493
|
+
* @param {Function} fallbackHandler - Fallback handler function
|
|
494
|
+
* @returns {object} Resolved resource
|
|
495
|
+
*/
|
|
496
|
+
resolveResource = (type, item = '', opts = {}, fallbackHandler) => {
|
|
497
|
+
const { trim, find, uniq } = this.app.lib._
|
|
498
|
+
const { fs } = this.app.lib
|
|
499
|
+
const env = this.app.bajo.config.env
|
|
500
|
+
if (env !== 'dev' && cache[item]) return cache[item]
|
|
501
|
+
|
|
502
|
+
let { ns, subSubNs, path } = this.getResource(item)
|
|
503
|
+
const ext = _path.extname(path)
|
|
504
|
+
path = trim(path, '/')
|
|
505
|
+
const dir = _path.dirname(path)
|
|
506
|
+
const base = _path.basename(path, ext)
|
|
507
|
+
const fallbackLang = this.app.bajo.config.intl.fallback
|
|
508
|
+
const exts = uniq([`.${fallbackLang}${ext}`, ext])
|
|
509
|
+
if (opts.lang) exts.unshift(`.${opts.lang}${ext}`)
|
|
510
|
+
let theme
|
|
511
|
+
if (opts.theme && this.app.waibuMpa && opts.req) theme = find(this.app.waibuMpa.themes, { name: opts.theme })
|
|
512
|
+
let file = this._findFile({ type, ns, subSubNs, dir, base, exts, theme, req: opts.req })
|
|
513
|
+
if (!file) {
|
|
514
|
+
if (fallbackHandler) file = fallbackHandler.call(this, { dir, base, exts, ns, subSubNs, type, theme, req: opts.req })
|
|
515
|
+
if (opts.default) {
|
|
516
|
+
const fname = this.getResource(opts.default).fullPath
|
|
517
|
+
if (fs.existsSync(fname)) file = fname
|
|
518
|
+
}
|
|
519
|
+
if (!file) throw this.error('cantFind%s%s', type, item)
|
|
520
|
+
}
|
|
521
|
+
const result = { file, ns, layout: opts.layout }
|
|
522
|
+
if (env !== 'dev') cache[item] = result
|
|
523
|
+
return result
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Resolve a layout by name.
|
|
528
|
+
*
|
|
529
|
+
* @method
|
|
530
|
+
* @param {string} item - Layout name
|
|
531
|
+
* @param {object} opts - Options object
|
|
532
|
+
* @returns {object} Resolved layout
|
|
533
|
+
*/
|
|
294
534
|
resolveLayout = (item = '', opts = {}) => {
|
|
295
|
-
const {
|
|
296
|
-
const fallbackHandler = ({ file, exts, ns, subSubNs, type, theme }) => {
|
|
297
|
-
const dir = ''
|
|
298
|
-
const base = 'default'
|
|
535
|
+
const fallbackHandler = ({ type, ns, subSubNs, dir, exts, theme, req }) => {
|
|
299
536
|
if (!this.config.layout.fallback) return false
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
const check = `${this.app.main.dir.pkg}/extend/${this.ns}/${type}/_${theme}`
|
|
303
|
-
file = filecheck.call(this, { dir, base, exts, check })
|
|
304
|
-
}
|
|
305
|
-
// check main: common
|
|
306
|
-
if (!file) {
|
|
307
|
-
const check = `${this.app.main.dir.pkg}/extend/${this.ns}/${type}`
|
|
308
|
-
file = filecheck.call(this, { dir, base, exts, check })
|
|
309
|
-
}
|
|
310
|
-
if (theme && !file) {
|
|
311
|
-
const otheme = find(this.app.waibuMpa.themes, { name: theme })
|
|
312
|
-
const check = `${otheme.plugin.dir.pkg}/extend/${this.ns}/extend/${this.ns}/${type}`
|
|
313
|
-
file = filecheck.call(this, { dir, base, exts, check })
|
|
314
|
-
}
|
|
315
|
-
// check fallback: common
|
|
316
|
-
if (!file) {
|
|
317
|
-
const check = `${this.app[ns].dir.pkg}/extend/${this.ns}/${subSubNs ? (subSubNs + '/') : ''}${type}`
|
|
318
|
-
file = filecheck.call(this, { dir, base, exts, check })
|
|
319
|
-
}
|
|
320
|
-
// check general fallback
|
|
321
|
-
if (!file) {
|
|
322
|
-
const check = `${this.dir.pkg}/extend/${this.ns}/${subSubNs ? (subSubNs + '/') : ''}${type}`
|
|
323
|
-
file = filecheck.call(this, { dir, base, exts, check })
|
|
324
|
-
}
|
|
325
|
-
return file
|
|
537
|
+
const base = 'default'
|
|
538
|
+
return this._findFile({ type, ns, subSubNs, dir, base, exts, theme, req })
|
|
326
539
|
}
|
|
327
540
|
|
|
328
|
-
return resolveResource
|
|
541
|
+
return this.resolveResource('layout', item, opts, fallbackHandler)
|
|
329
542
|
}
|
|
330
543
|
|
|
544
|
+
/**
|
|
545
|
+
* Resolve a partial by name.
|
|
546
|
+
*
|
|
547
|
+
* @method
|
|
548
|
+
* @param {string} item - Partial name
|
|
549
|
+
* @param {object} opts - Options object
|
|
550
|
+
* @returns {object} Resolved partial
|
|
551
|
+
*/
|
|
331
552
|
resolvePartial = (item = '', opts = {}) => {
|
|
332
|
-
return resolveResource
|
|
553
|
+
return this.resolveResource('partial', item, opts)
|
|
333
554
|
}
|
|
334
555
|
|
|
556
|
+
/**
|
|
557
|
+
* Resolve a template by name.
|
|
558
|
+
*
|
|
559
|
+
* @method
|
|
560
|
+
* @param {string} item - Template name
|
|
561
|
+
* @param {object} opts - Options object
|
|
562
|
+
* @returns {object} Resolved template
|
|
563
|
+
*/
|
|
335
564
|
resolveTemplate = (item = '', opts = {}) => {
|
|
336
|
-
return resolveResource
|
|
565
|
+
return this.resolveResource('template', item, opts)
|
|
337
566
|
}
|
|
338
567
|
|
|
568
|
+
/**
|
|
569
|
+
* Split content into front matter and body.
|
|
570
|
+
*
|
|
571
|
+
* @method
|
|
572
|
+
* @param {string} input - Input string or file path
|
|
573
|
+
* @param {boolean} readFile - Whether to read the file
|
|
574
|
+
* @returns {object} Object containing frontMatter and content
|
|
575
|
+
*/
|
|
339
576
|
splitContent = (input, readFile) => {
|
|
340
577
|
const { fs } = this.app.lib
|
|
341
578
|
const start = '---\n'
|
|
@@ -358,13 +595,35 @@ async function factory (pkgName) {
|
|
|
358
595
|
return { frontMatter, content }
|
|
359
596
|
}
|
|
360
597
|
|
|
361
|
-
|
|
362
|
-
|
|
598
|
+
/**
|
|
599
|
+
* Get an excerpt from the content.
|
|
600
|
+
*
|
|
601
|
+
* @method
|
|
602
|
+
* @param {string} content - Content string
|
|
603
|
+
* @param {number} maxWords - Maximum number of words
|
|
604
|
+
* @param {string} trailChars - Trailing characters
|
|
605
|
+
* @returns {string} Excerpt
|
|
606
|
+
* @see {@link https://medium.com/@paulohfev/problem-solving-how-to-create-an-excerpt-fdb048687928|Problem Solving: How to Create an Excerpt}
|
|
607
|
+
*/
|
|
608
|
+
getExcerpt = (content = '', maxWords = 50, trailChars = '...') => {
|
|
363
609
|
const listOfWords = content.trim().split(' ')
|
|
364
610
|
const truncatedContent = listOfWords.slice(0, maxWords).join(' ')
|
|
365
611
|
const excerpt = truncatedContent + trailChars
|
|
366
|
-
|
|
367
|
-
|
|
612
|
+
return listOfWords.length > maxWords ? excerpt : content
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
/**
|
|
616
|
+
* Get a truncated version of the content.
|
|
617
|
+
*
|
|
618
|
+
* @method
|
|
619
|
+
* @param {string} content - Content string
|
|
620
|
+
* @param {number} maxChars - Maximum number of characters
|
|
621
|
+
* @param {string} trailChars - Trailing characters
|
|
622
|
+
* @returns {string} Truncated content
|
|
623
|
+
*/
|
|
624
|
+
getTruncated = (content = '', maxChars = 50, trailChars = '...') => {
|
|
625
|
+
const truncated = content.slice(0, maxChars)
|
|
626
|
+
return truncated.length !== content.length ? (truncated + trailChars) : content
|
|
368
627
|
}
|
|
369
628
|
}
|
|
370
629
|
|
|
@@ -372,4 +631,4 @@ async function factory (pkgName) {
|
|
|
372
631
|
}
|
|
373
632
|
|
|
374
633
|
export default factory
|
|
375
|
-
</code></pre></article></section></div></div></div><div class="search-container" id="PkfLWpAbet" style="display:none"><div class="wrapper" id="iCxFxjkHbP"><button class="icon-button search-close-button" id="VjLlGakifb" aria-label="close search"><svg><use xlink:href="#close-icon"></use></svg></button><div class="search-box-c"><svg><use xlink:href="#search-icon"></use></svg> <input type="text" id="vpcKVYIppa" class="search-input" placeholder="Search..." autofocus></div><div class="search-result-c" id="fWwVHRuDuN"><span class="search-result-c-text">Type anything to view search result</span></div></div></div><div class="mobile-menu-icon-container"><button class="icon-button" id="mobile-menu" data-isopen="false" aria-label="menu"><svg><use xlink:href="#menu-icon"></use></svg></button></div><div id="mobile-sidebar" class="mobile-sidebar-container"><div class="mobile-sidebar-wrapper"><a href="/" class="sidebar-title sidebar-title-anchor">BajoTemplate API</a><div class="mobile-nav-links"><div class="navbar-item"><a id="" href="https://www.npmjs.com/package/bajo-template" target="">NPM</a></div><div class="navbar-item"><a id="" href="https://github.com/ardhi/bajo-template" target="">Github</a></div><div class="navbar-item"><a id="" href="https://bajo.app" target="">Bajo</a></div></div><div class="mobile-sidebar-items-c"><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-classes"><div>Classes</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="BajoTemplate.html">BajoTemplate</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-global"><div>Global</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="global.html#factory">factory</a></div></div></div><div class="mobile-navbar-actions"><div class="navbar-right-item"><button class="icon-button search-button" aria-label="open-search"><svg><use xlink:href="#search-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button theme-toggle" aria-label="toggle-theme"><svg><use class="theme-svg-use" xlink:href="#dark-theme-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button font-size" aria-label="change-font-size"><svg><use xlink:href="#font-size-icon"></use></svg></button></div></div></div></div><script type="text/javascript" src="scripts/core.min.js"></script><script src="scripts/search.min.js" defer="defer"></script><script src="scripts/third-party/fuse.js" defer="defer"></script><script type="text/javascript">var tocbotInstance=tocbot.init({tocSelector:"#eed4d2a0bfd64539bb9df78095dec881",contentSelector:".main-content",headingSelector:"h1, h2, h3",hasInnerContainers:!0,scrollContainer:".main-content",headingsOffset:130,onClick:bringLinkToView})</script></body></html>
|
|
634
|
+
</code></pre></article></section></div></div></div><div class="search-container" id="PkfLWpAbet" style="display:none"><div class="wrapper" id="iCxFxjkHbP"><button class="icon-button search-close-button" id="VjLlGakifb" aria-label="close search"><svg><use xlink:href="#close-icon"></use></svg></button><div class="search-box-c"><svg><use xlink:href="#search-icon"></use></svg> <input type="text" id="vpcKVYIppa" class="search-input" placeholder="Search..." autofocus></div><div class="search-result-c" id="fWwVHRuDuN"><span class="search-result-c-text">Type anything to view search result</span></div></div></div><div class="mobile-menu-icon-container"><button class="icon-button" id="mobile-menu" data-isopen="false" aria-label="menu"><svg><use xlink:href="#menu-icon"></use></svg></button></div><div id="mobile-sidebar" class="mobile-sidebar-container"><div class="mobile-sidebar-wrapper"><a href="/" class="sidebar-title sidebar-title-anchor">BajoTemplate API</a><div class="mobile-nav-links"><div class="navbar-item"><a id="" href="https://www.npmjs.com/package/bajo-template" target="">NPM</a></div><div class="navbar-item"><a id="" href="https://github.com/ardhi/bajo-template" target="">Github</a></div><div class="navbar-item"><a id="" href="https://bajo.app" target="">Bajo</a></div></div><div class="mobile-sidebar-items-c"><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-classes"><div>Classes</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="BajoTemplate.html">BajoTemplate</a></div><div class="sidebar-section-children"><a href="BajoTemplate.html">BajoTemplate</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-modules"><div>Modules</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="module-Imports.html">Imports</a></div></div><div class="sidebar-section-title with-arrow" data-isopen="false" id="sidebar-global"><div>Global</div><svg><use xlink:href="#down-icon"></use></svg></div><div class="sidebar-section-children-container"><div class="sidebar-section-children"><a href="global.html#TImportsResult">TImportsResult</a></div><div class="sidebar-section-children"><a href="global.html#factory">factory</a></div></div></div><div class="mobile-navbar-actions"><div class="navbar-right-item"><button class="icon-button search-button" aria-label="open-search"><svg><use xlink:href="#search-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button theme-toggle" aria-label="toggle-theme"><svg><use class="theme-svg-use" xlink:href="#dark-theme-icon"></use></svg></button></div><div class="navbar-right-item"><button class="icon-button font-size" aria-label="change-font-size"><svg><use xlink:href="#font-size-icon"></use></svg></button></div></div></div></div><script type="text/javascript" src="scripts/core.min.js"></script><script src="scripts/search.min.js" defer="defer"></script><script src="scripts/third-party/fuse.js" defer="defer"></script><script type="text/javascript">var tocbotInstance=tocbot.init({tocSelector:"#eed4d2a0bfd64539bb9df78095dec881",contentSelector:".main-content",headingSelector:"h1, h2, h3",hasInnerContainers:!0,scrollContainer:".main-content",headingsOffset:130,onClick:bringLinkToView})</script></body></html>
|