honkit 3.6.21

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.
Files changed (278) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +10 -0
  3. package/bin/honkit.js +8 -0
  4. package/lib/BuildGenerator.js +2 -0
  5. package/lib/__tests__/__fixtures__/honkit/book.js +9 -0
  6. package/lib/__tests__/githon.js +11 -0
  7. package/lib/__tests__/init.js +19 -0
  8. package/lib/__tests__/snapshot-honkit.js +45 -0
  9. package/lib/api/decodeConfig.js +17 -0
  10. package/lib/api/decodeGlobal.js +21 -0
  11. package/lib/api/decodePage.js +41 -0
  12. package/lib/api/deprecate.js +113 -0
  13. package/lib/api/encodeConfig.js +30 -0
  14. package/lib/api/encodeGlobal.js +226 -0
  15. package/lib/api/encodeNavigation.js +60 -0
  16. package/lib/api/encodePage.js +37 -0
  17. package/lib/api/encodeProgress.js +58 -0
  18. package/lib/api/encodeSummary.js +52 -0
  19. package/lib/api/index.js +15 -0
  20. package/lib/bin.js +46 -0
  21. package/lib/browser.js +41 -0
  22. package/lib/cli/build.js +36 -0
  23. package/lib/cli/buildEbook.js +63 -0
  24. package/lib/cli/getBook.js +24 -0
  25. package/lib/cli/getOutputFolder.js +19 -0
  26. package/lib/cli/index.js +11 -0
  27. package/lib/cli/init.js +17 -0
  28. package/lib/cli/options.js +38 -0
  29. package/lib/cli/parse.js +65 -0
  30. package/lib/cli/serve.js +184 -0
  31. package/lib/cli/server.js +116 -0
  32. package/lib/cli/watch.js +35 -0
  33. package/lib/constants/__tests__/configSchema.js +41 -0
  34. package/lib/constants/configDefault.js +9 -0
  35. package/lib/constants/configFiles.js +3 -0
  36. package/lib/constants/configSchema.js +235 -0
  37. package/lib/constants/defaultBlocks.js +48 -0
  38. package/lib/constants/defaultFilters.js +18 -0
  39. package/lib/constants/defaultPlugins.js +20 -0
  40. package/lib/constants/extsAsciidoc.js +3 -0
  41. package/lib/constants/extsMarkdown.js +3 -0
  42. package/lib/constants/ignoreFiles.js +3 -0
  43. package/lib/constants/pluginAssetsFolder.js +3 -0
  44. package/lib/constants/pluginHooks.js +3 -0
  45. package/lib/constants/pluginPrefix.js +3 -0
  46. package/lib/constants/pluginResources.js +7 -0
  47. package/lib/constants/templatesFolder.js +3 -0
  48. package/lib/constants/themePrefix.js +3 -0
  49. package/lib/fs/__tests__/mock.js +68 -0
  50. package/lib/fs/mock.js +88 -0
  51. package/lib/fs/node.js +44 -0
  52. package/lib/honkit.js +34 -0
  53. package/lib/index.js +17 -0
  54. package/lib/init.js +79 -0
  55. package/lib/json/encodeBook.js +41 -0
  56. package/lib/json/encodeBookWithPage.js +25 -0
  57. package/lib/json/encodeFile.js +20 -0
  58. package/lib/json/encodeGlossary.js +22 -0
  59. package/lib/json/encodeGlossaryEntry.js +16 -0
  60. package/lib/json/encodeLanguages.js +29 -0
  61. package/lib/json/encodeOutput.js +26 -0
  62. package/lib/json/encodeOutputWithPage.js +26 -0
  63. package/lib/json/encodePage.js +38 -0
  64. package/lib/json/encodeReadme.js +19 -0
  65. package/lib/json/encodeSummary.js +22 -0
  66. package/lib/json/encodeSummaryArticle.js +25 -0
  67. package/lib/json/encodeSummaryPart.js +19 -0
  68. package/lib/json/index.js +27 -0
  69. package/lib/models/__tests__/config.js +164 -0
  70. package/lib/models/__tests__/glossary.js +38 -0
  71. package/lib/models/__tests__/glossaryEntry.js +16 -0
  72. package/lib/models/__tests__/page.js +26 -0
  73. package/lib/models/__tests__/plugin.js +26 -0
  74. package/lib/models/__tests__/pluginDependency.js +71 -0
  75. package/lib/models/__tests__/summary.js +115 -0
  76. package/lib/models/__tests__/summaryArticle.js +62 -0
  77. package/lib/models/__tests__/summaryPart.js +24 -0
  78. package/lib/models/__tests__/templateBlock.js +172 -0
  79. package/lib/models/__tests__/templateEngine.js +49 -0
  80. package/lib/models/book.js +312 -0
  81. package/lib/models/config.js +152 -0
  82. package/lib/models/file.js +83 -0
  83. package/lib/models/fs.js +255 -0
  84. package/lib/models/glossary.js +90 -0
  85. package/lib/models/glossaryEntry.js +34 -0
  86. package/lib/models/hash.js +191 -0
  87. package/lib/models/ignore.js +40 -0
  88. package/lib/models/language.js +22 -0
  89. package/lib/models/languages.js +64 -0
  90. package/lib/models/output.js +109 -0
  91. package/lib/models/page.js +91 -0
  92. package/lib/models/parser.js +102 -0
  93. package/lib/models/plugin.js +139 -0
  94. package/lib/models/pluginDependency.js +192 -0
  95. package/lib/models/readme.js +38 -0
  96. package/lib/models/summary.js +199 -0
  97. package/lib/models/summaryArticle.js +169 -0
  98. package/lib/models/summaryPart.js +53 -0
  99. package/lib/models/templateBlock.js +230 -0
  100. package/lib/models/templateEngine.js +115 -0
  101. package/lib/models/templateOutput.js +36 -0
  102. package/lib/models/templateShortcut.js +65 -0
  103. package/lib/modifiers/config/__tests__/addPlugin.js +30 -0
  104. package/lib/modifiers/config/__tests__/removePlugin.js +29 -0
  105. package/lib/modifiers/config/__tests__/togglePlugin.js +28 -0
  106. package/lib/modifiers/config/addPlugin.js +27 -0
  107. package/lib/modifiers/config/editPlugin.js +13 -0
  108. package/lib/modifiers/config/getPluginConfig.js +22 -0
  109. package/lib/modifiers/config/hasPlugin.js +15 -0
  110. package/lib/modifiers/config/index.js +21 -0
  111. package/lib/modifiers/config/isDefaultPlugin.js +17 -0
  112. package/lib/modifiers/config/removePlugin.js +27 -0
  113. package/lib/modifiers/config/togglePlugin.js +32 -0
  114. package/lib/modifiers/index.js +11 -0
  115. package/lib/modifiers/summary/__tests__/editPartTitle.js +41 -0
  116. package/lib/modifiers/summary/__tests__/insertArticle.js +72 -0
  117. package/lib/modifiers/summary/__tests__/insertPart.js +56 -0
  118. package/lib/modifiers/summary/__tests__/mergeAtLevel.js +45 -0
  119. package/lib/modifiers/summary/__tests__/moveArticle.js +66 -0
  120. package/lib/modifiers/summary/__tests__/moveArticleAfter.js +75 -0
  121. package/lib/modifiers/summary/__tests__/removeArticle.js +54 -0
  122. package/lib/modifiers/summary/editArticleRef.js +20 -0
  123. package/lib/modifiers/summary/editArticleTitle.js +20 -0
  124. package/lib/modifiers/summary/editPartTitle.js +21 -0
  125. package/lib/modifiers/summary/index.js +27 -0
  126. package/lib/modifiers/summary/indexArticleLevels.js +21 -0
  127. package/lib/modifiers/summary/indexLevels.js +18 -0
  128. package/lib/modifiers/summary/indexPartLevels.js +25 -0
  129. package/lib/modifiers/summary/insertArticle.js +47 -0
  130. package/lib/modifiers/summary/insertPart.js +21 -0
  131. package/lib/modifiers/summary/mergeAtLevel.js +68 -0
  132. package/lib/modifiers/summary/moveArticle.js +27 -0
  133. package/lib/modifiers/summary/moveArticleAfter.js +59 -0
  134. package/lib/modifiers/summary/removeArticle.js +36 -0
  135. package/lib/modifiers/summary/removePart.js +18 -0
  136. package/lib/modifiers/summary/unshiftArticle.js +27 -0
  137. package/lib/output/__tests__/ebook.js +17 -0
  138. package/lib/output/__tests__/json.js +43 -0
  139. package/lib/output/__tests__/plugin-hooks.js +57 -0
  140. package/lib/output/__tests__/website.js +121 -0
  141. package/lib/output/callHook.js +51 -0
  142. package/lib/output/callPageHook.js +23 -0
  143. package/lib/output/createTemplateEngine.js +42 -0
  144. package/lib/output/ebook/getConvertOptions.js +69 -0
  145. package/lib/output/ebook/getCoverPath.js +30 -0
  146. package/lib/output/ebook/getPDFTemplate.js +40 -0
  147. package/lib/output/ebook/index.js +16 -0
  148. package/lib/output/ebook/onFinish.js +84 -0
  149. package/lib/output/ebook/onPage.js +23 -0
  150. package/lib/output/ebook/options.js +17 -0
  151. package/lib/output/generateAssets.js +26 -0
  152. package/lib/output/generateBook.js +167 -0
  153. package/lib/output/generatePage.js +90 -0
  154. package/lib/output/generatePages.js +52 -0
  155. package/lib/output/getModifiers.js +63 -0
  156. package/lib/output/helper/fileToOutput.js +30 -0
  157. package/lib/output/helper/fileToURL.js +30 -0
  158. package/lib/output/helper/index.js +3 -0
  159. package/lib/output/helper/resolveFileToURL.js +25 -0
  160. package/lib/output/helper/writeFile.js +26 -0
  161. package/lib/output/index.js +27 -0
  162. package/lib/output/json/index.js +14 -0
  163. package/lib/output/json/onFinish.js +39 -0
  164. package/lib/output/json/onPage.js +37 -0
  165. package/lib/output/json/options.js +11 -0
  166. package/lib/output/modifiers/__tests__/addHeadingId.js +23 -0
  167. package/lib/output/modifiers/__tests__/annotateText.js +39 -0
  168. package/lib/output/modifiers/__tests__/fetchRemoteImages.js +32 -0
  169. package/lib/output/modifiers/__tests__/highlightCode.js +48 -0
  170. package/lib/output/modifiers/__tests__/inlinePng.js +22 -0
  171. package/lib/output/modifiers/__tests__/inlineSvg.js +42 -0
  172. package/lib/output/modifiers/__tests__/resolveImages.js +39 -0
  173. package/lib/output/modifiers/__tests__/resolveLinks.js +81 -0
  174. package/lib/output/modifiers/__tests__/svgToImg.js +29 -0
  175. package/lib/output/modifiers/addHeadingId.js +26 -0
  176. package/lib/output/modifiers/annotateText.js +76 -0
  177. package/lib/output/modifiers/editHTMLElement.js +17 -0
  178. package/lib/output/modifiers/fetchRemoteImages.js +42 -0
  179. package/lib/output/modifiers/highlightCode.js +59 -0
  180. package/lib/output/modifiers/index.js +26 -0
  181. package/lib/output/modifiers/inlineAssets.js +29 -0
  182. package/lib/output/modifiers/inlinePng.js +43 -0
  183. package/lib/output/modifiers/inlineSvg.js +39 -0
  184. package/lib/output/modifiers/modifyHTML.js +27 -0
  185. package/lib/output/modifiers/resolveImages.js +30 -0
  186. package/lib/output/modifiers/resolveLinks.js +46 -0
  187. package/lib/output/modifiers/svgToImg.js +58 -0
  188. package/lib/output/page-cache.js +15 -0
  189. package/lib/output/prepareAssets.js +22 -0
  190. package/lib/output/preparePages.js +25 -0
  191. package/lib/output/preparePlugins.js +36 -0
  192. package/lib/output/testing/createMock.js +39 -0
  193. package/lib/output/testing/generateMock.js +55 -0
  194. package/lib/output/website/__tests__/i18n.js +40 -0
  195. package/lib/output/website/copyPluginAssets.js +92 -0
  196. package/lib/output/website/createTemplateEngine.js +136 -0
  197. package/lib/output/website/index.js +22 -0
  198. package/lib/output/website/listSearchPaths.js +20 -0
  199. package/lib/output/website/onAsset.js +29 -0
  200. package/lib/output/website/onFinish.js +35 -0
  201. package/lib/output/website/onInit.js +19 -0
  202. package/lib/output/website/onPage.js +73 -0
  203. package/lib/output/website/options.js +15 -0
  204. package/lib/output/website/prepareI18n.js +28 -0
  205. package/lib/output/website/prepareResources.js +49 -0
  206. package/lib/output/website/state.js +20 -0
  207. package/lib/parse/__tests__/listAssets.js +63 -0
  208. package/lib/parse/__tests__/parseBook.js +77 -0
  209. package/lib/parse/__tests__/parseGlossary.js +32 -0
  210. package/lib/parse/__tests__/parseIgnore.js +37 -0
  211. package/lib/parse/__tests__/parsePageFromString.js +34 -0
  212. package/lib/parse/__tests__/parseReadme.js +33 -0
  213. package/lib/parse/__tests__/parseSummary.js +30 -0
  214. package/lib/parse/findParsableFile.js +33 -0
  215. package/lib/parse/index.js +31 -0
  216. package/lib/parse/listAssets.js +36 -0
  217. package/lib/parse/lookupStructureFile.js +21 -0
  218. package/lib/parse/parseBook.js +68 -0
  219. package/lib/parse/parseConfig.js +52 -0
  220. package/lib/parse/parseGlossary.js +25 -0
  221. package/lib/parse/parseIgnore.js +43 -0
  222. package/lib/parse/parseLanguages.js +26 -0
  223. package/lib/parse/parsePage.js +21 -0
  224. package/lib/parse/parsePageFromString.js +25 -0
  225. package/lib/parse/parsePagesList.js +80 -0
  226. package/lib/parse/parseReadme.js +26 -0
  227. package/lib/parse/parseStructureFile.js +64 -0
  228. package/lib/parse/parseSummary.js +43 -0
  229. package/lib/parse/validateConfig.js +31 -0
  230. package/lib/parse/walkSummary.js +34 -0
  231. package/lib/parsers.js +60 -0
  232. package/lib/plugins/PluginResolver.js +84 -0
  233. package/lib/plugins/__tests__/findInstalled.js +25 -0
  234. package/lib/plugins/__tests__/listDependencies.js +28 -0
  235. package/lib/plugins/__tests__/sortDependencies.js +28 -0
  236. package/lib/plugins/__tests__/validatePlugin.js +99 -0
  237. package/lib/plugins/findInstalled.js +87 -0
  238. package/lib/plugins/index.js +17 -0
  239. package/lib/plugins/listBlocks.js +19 -0
  240. package/lib/plugins/listDependencies.js +33 -0
  241. package/lib/plugins/listDepsForBook.js +20 -0
  242. package/lib/plugins/listFilters.js +18 -0
  243. package/lib/plugins/listResources.js +44 -0
  244. package/lib/plugins/loadForBook.js +38 -0
  245. package/lib/plugins/loadPlugin.js +89 -0
  246. package/lib/plugins/package-name-util.js +48 -0
  247. package/lib/plugins/sortDependencies.js +30 -0
  248. package/lib/plugins/toNames.js +16 -0
  249. package/lib/plugins/validateConfig.js +60 -0
  250. package/lib/plugins/validatePlugin.js +36 -0
  251. package/lib/templating/__tests__/conrefsLoader.js +93 -0
  252. package/lib/templating/__tests__/postRender.js +52 -0
  253. package/lib/templating/__tests__/replaceShortcuts.js +27 -0
  254. package/lib/templating/conrefsLoader.js +88 -0
  255. package/lib/templating/index.js +19 -0
  256. package/lib/templating/listShortcuts.js +29 -0
  257. package/lib/templating/postRender.js +46 -0
  258. package/lib/templating/render.js +41 -0
  259. package/lib/templating/renderFile.js +39 -0
  260. package/lib/templating/replaceShortcuts.js +36 -0
  261. package/lib/templating/themesLoader.js +98 -0
  262. package/lib/utils/__tests__/git.js +47 -0
  263. package/lib/utils/__tests__/location.js +81 -0
  264. package/lib/utils/__tests__/path.js +19 -0
  265. package/lib/utils/command.js +104 -0
  266. package/lib/utils/error.js +88 -0
  267. package/lib/utils/fs.js +163 -0
  268. package/lib/utils/genKey.js +13 -0
  269. package/lib/utils/git.js +116 -0
  270. package/lib/utils/images.js +22 -0
  271. package/lib/utils/location.js +129 -0
  272. package/lib/utils/logger.js +158 -0
  273. package/lib/utils/mergeDefaults.js +18 -0
  274. package/lib/utils/path.js +62 -0
  275. package/lib/utils/promise.js +142 -0
  276. package/lib/utils/reducedObject.js +31 -0
  277. package/lib/utils/timing.js +90 -0
  278. package/package.json +112 -0
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const promise_1 = __importDefault(require("../utils/promise"));
7
+ const timing_1 = __importDefault(require("../utils/timing"));
8
+ const templateOutput_1 = __importDefault(require("../models/templateOutput"));
9
+ const replaceShortcuts_1 = __importDefault(require("./replaceShortcuts"));
10
+ /**
11
+ * Render a template
12
+ *
13
+ * @param {TemplateEngine} engine
14
+ * @param {string} filePath: absolute path for the loader
15
+ * @param {string} content
16
+ * @param {Object} context (optional)
17
+ * @return {Promise<TemplateOutput>}
18
+ */
19
+ function renderTemplate(engine, filePath, content, context) {
20
+ context = context || {};
21
+ // Mutable objects to contains all blocks requiring post-processing
22
+ const blocks = {};
23
+ // Create nunjucks environment
24
+ const env = engine.toNunjucks(blocks);
25
+ // Replace shortcuts from plugin's blocks
26
+ content = replaceShortcuts_1.default(engine.getBlocks(), filePath, content);
27
+ return timing_1.default.measure("template.render", promise_1.default.nfcall(env.renderString.bind(env), content, context, {
28
+ path: filePath,
29
+ })
30
+ .then((content) => {
31
+ return templateOutput_1.default.create(content, blocks);
32
+ })
33
+ .catch((error) => {
34
+ console.log("env:", env);
35
+ console.log("context:", context);
36
+ console.log("content:", content);
37
+ console.error("rendering error:", error);
38
+ return promise_1.default.reject(error);
39
+ }));
40
+ }
41
+ exports.default = renderTemplate;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const promise_1 = __importDefault(require("../utils/promise"));
7
+ const error_1 = __importDefault(require("../utils/error"));
8
+ const render_1 = __importDefault(require("./render"));
9
+ /**
10
+ * Render a template
11
+ *
12
+ * @param {TemplateEngine} engine
13
+ * @param {string} filePath
14
+ * @param {Object} context
15
+ * @return {Promise<TemplateOutput>}
16
+ */
17
+ function renderTemplateFile(engine, filePath, context) {
18
+ const loader = engine.getLoader();
19
+ // Resolve the filePath
20
+ const resolvedFilePath = loader.resolve(null, filePath);
21
+ return promise_1.default()
22
+ .then(() => {
23
+ if (!loader.async) {
24
+ return loader.getSource(resolvedFilePath);
25
+ }
26
+ const deferred = promise_1.default.defer();
27
+ loader.getSource(resolvedFilePath, deferred.makeNodeResolver());
28
+ return deferred.promise;
29
+ })
30
+ .then((result) => {
31
+ if (!result) {
32
+ throw error_1.default.TemplateError(new Error("Not found"), {
33
+ filename: filePath,
34
+ });
35
+ }
36
+ return render_1.default(engine, result.path, result.src, context);
37
+ });
38
+ }
39
+ exports.default = renderTemplateFile;
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const escape_string_regexp_1 = __importDefault(require("escape-string-regexp"));
7
+ const listShortcuts_1 = __importDefault(require("./listShortcuts"));
8
+ /**
9
+ * Apply a shortcut of block to a template
10
+ * @param {string} content
11
+ * @param {Shortcut} shortcut
12
+ * @return {string}
13
+ */
14
+ function applyShortcut(content, shortcut) {
15
+ const start = shortcut.getStart();
16
+ const end = shortcut.getEnd();
17
+ const tagStart = shortcut.getStartTag();
18
+ const tagEnd = shortcut.getEndTag();
19
+ const regex = new RegExp(`${escape_string_regexp_1.default(start)}([\\s\\S]*?[^\\$])${escape_string_regexp_1.default(end)}`, "g");
20
+ return content.replace(regex, (all, match) => {
21
+ return `{% ${tagStart} %}${match}{% ${tagEnd} %}`;
22
+ });
23
+ }
24
+ /**
25
+ * Replace shortcuts from blocks in a string
26
+ *
27
+ * @param {List<TemplateBlock>} engine
28
+ * @param {string} filePath
29
+ * @param {string} content
30
+ * @return {string}
31
+ */
32
+ function replaceShortcuts(blocks, filePath, content) {
33
+ const shortcuts = listShortcuts_1.default(blocks, filePath);
34
+ return shortcuts.reduce(applyShortcut, content);
35
+ }
36
+ exports.default = replaceShortcuts;
@@ -0,0 +1,98 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const immutable_1 = __importDefault(require("immutable"));
7
+ const nunjucks_1 = __importDefault(require("nunjucks"));
8
+ const fs_1 = __importDefault(require("fs"));
9
+ const path_1 = __importDefault(require("path"));
10
+ const path_2 = __importDefault(require("../utils/path"));
11
+ const ThemesLoader = nunjucks_1.default.Loader.extend({
12
+ // @ts-expect-error: Property 'extend' does not exist on type 'typeof Loader'.
13
+ init: function (searchPaths) {
14
+ this.searchPaths = immutable_1.default.List(searchPaths).map(path_1.default.normalize);
15
+ },
16
+ /**
17
+ * Read source of a resolved filepath
18
+ * @param {string}
19
+ * @return {Object}
20
+ */
21
+ getSource: function (fullpath) {
22
+ if (!fullpath)
23
+ return null;
24
+ fullpath = this.resolve(null, fullpath);
25
+ const templateName = this.getTemplateName(fullpath);
26
+ if (!fullpath) {
27
+ return null;
28
+ }
29
+ let src = fs_1.default.readFileSync(fullpath, "utf-8");
30
+ src = `{% do %}var template = template || {}; template.stack = template.stack || []; template.stack.push(template.self); template.self = ${JSON.stringify(templateName)}{% enddo %}\n${src}\n{% do %}template.self = template.stack.pop();{% enddo %}`;
31
+ return {
32
+ src: src,
33
+ path: fullpath,
34
+ noCache: true,
35
+ };
36
+ },
37
+ /**
38
+ * Nunjucks calls "isRelative" to determine when to call "resolve".
39
+ * We handle absolute paths ourselves in ".resolve" so we always return true
40
+ */
41
+ isRelative: function () {
42
+ return true;
43
+ },
44
+ /**
45
+ * Get original search path containing a template
46
+ * @param {string} filepath
47
+ * @return {string} searchPath
48
+ */
49
+ getSearchPath: function (filepath) {
50
+ return this.searchPaths
51
+ .sortBy((s) => {
52
+ return -s.length;
53
+ })
54
+ .find((basePath) => {
55
+ return filepath && filepath.indexOf(basePath) === 0;
56
+ });
57
+ },
58
+ /**
59
+ * Get template name from a filepath
60
+ * @param {string} filepath
61
+ * @return {string} name
62
+ */
63
+ getTemplateName: function (filepath) {
64
+ const originalSearchPath = this.getSearchPath(filepath);
65
+ return originalSearchPath ? path_1.default.relative(originalSearchPath, filepath) : null;
66
+ },
67
+ /**
68
+ * Resolve a template from a current template
69
+ * @param {String|null} from
70
+ * @param {string} to
71
+ * @return {String|null}
72
+ */
73
+ resolve: function (from, to) {
74
+ let searchPaths = this.searchPaths;
75
+ // Relative template like "./test.html"
76
+ if (path_2.default.isPureRelative(to) && from) {
77
+ return path_1.default.resolve(path_1.default.dirname(from), to);
78
+ }
79
+ // Determine in which search folder we currently are
80
+ const originalSearchPath = this.getSearchPath(from);
81
+ const originalFilename = this.getTemplateName(from);
82
+ // If we are including same file from a different search path
83
+ // Slice the search paths to avoid including from previous ones
84
+ if (originalFilename == to) {
85
+ const currentIndex = searchPaths.indexOf(originalSearchPath);
86
+ searchPaths = searchPaths.slice(currentIndex + 1);
87
+ }
88
+ // Absolute template to resolve in root folder
89
+ const resultFolder = searchPaths.find((basePath) => {
90
+ const p = path_1.default.resolve(basePath, to);
91
+ return p.indexOf(basePath) === 0 && fs_1.default.existsSync(p);
92
+ });
93
+ if (!resultFolder)
94
+ return null;
95
+ return path_1.default.resolve(resultFolder, to);
96
+ },
97
+ });
98
+ exports.default = ThemesLoader;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const path_1 = __importDefault(require("path"));
7
+ const git_1 = __importDefault(require("../git"));
8
+ describe("Git", () => {
9
+ jest.setTimeout(30 * 1000);
10
+ describe("URL parsing", () => {
11
+ test("should correctly validate git urls", () => {
12
+ // HTTPS
13
+ expect(git_1.default.isUrl("git+https://github.com/Hello/world.git")).toBeTruthy();
14
+ // SSH
15
+ expect(git_1.default.isUrl("git+git@github.com:GitbookIO/gitbook.git/directory/README.md#e1594cde2c32e4ff48f6c4eff3d3d461743d74e1")).toBeTruthy();
16
+ // Non valid
17
+ expect(git_1.default.isUrl("https://github.com/Hello/world.git")).toBeFalsy();
18
+ expect(git_1.default.isUrl("README.md")).toBeFalsy();
19
+ });
20
+ test("should parse HTTPS urls", () => {
21
+ const parts = git_1.default.parseUrl("git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test.md");
22
+ expect(parts.host).toBe("https://gist.github.com/69ea4542e4c8967d2fa7.git");
23
+ expect(parts.ref).toBe(null);
24
+ expect(parts.filepath).toBe("test.md");
25
+ });
26
+ test("should parse HTTPS urls with a reference", () => {
27
+ const parts = git_1.default.parseUrl("git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test.md#1.0.0");
28
+ expect(parts.host).toBe("https://gist.github.com/69ea4542e4c8967d2fa7.git");
29
+ expect(parts.ref).toBe("1.0.0");
30
+ expect(parts.filepath).toBe("test.md");
31
+ });
32
+ test("should parse SSH urls", () => {
33
+ const parts = git_1.default.parseUrl("git+git@github.com:GitbookIO/gitbook.git/directory/README.md#e1594cde2c32e4ff48f6c4eff3d3d461743d74e1");
34
+ expect(parts.host).toBe("git@github.com:GitbookIO/gitbook.git");
35
+ expect(parts.ref).toBe("e1594cde2c32e4ff48f6c4eff3d3d461743d74e1");
36
+ expect(parts.filepath).toBe("directory/README.md");
37
+ });
38
+ });
39
+ describe("Cloning and resolving", () => {
40
+ test("should clone an HTTPS url", () => {
41
+ const git = new git_1.default();
42
+ return git.resolve("git+https://gist.github.com/69ea4542e4c8967d2fa7.git/test.md").then((filename) => {
43
+ expect(path_1.default.extname(filename)).toBe(".md");
44
+ });
45
+ });
46
+ });
47
+ });
@@ -0,0 +1,81 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const location_1 = __importDefault(require("../location"));
7
+ describe("LocationUtils", () => {
8
+ test("should correctly test external location", () => {
9
+ expect(location_1.default.isExternal("http://google.fr")).toBe(true);
10
+ expect(location_1.default.isExternal("https://google.fr")).toBe(true);
11
+ expect(location_1.default.isExternal("test.md")).toBe(false);
12
+ expect(location_1.default.isExternal("folder/test.md")).toBe(false);
13
+ expect(location_1.default.isExternal("/folder/test.md")).toBe(false);
14
+ expect(location_1.default.isExternal("data:image/png")).toBe(false);
15
+ });
16
+ test("should correctly test data:uri location", () => {
17
+ expect(location_1.default.isDataURI("data:image/png")).toBe(true);
18
+ expect(location_1.default.isDataURI("http://google.fr")).toBe(false);
19
+ expect(location_1.default.isDataURI("https://google.fr")).toBe(false);
20
+ expect(location_1.default.isDataURI("test.md")).toBe(false);
21
+ expect(location_1.default.isDataURI("data.md")).toBe(false);
22
+ });
23
+ test("should correctly detect anchor location", () => {
24
+ expect(location_1.default.isAnchor("#test")).toBe(true);
25
+ expect(location_1.default.isAnchor(" #test")).toBe(true);
26
+ expect(location_1.default.isAnchor("https://google.fr#test")).toBe(false);
27
+ expect(location_1.default.isAnchor("test.md#test")).toBe(false);
28
+ });
29
+ describe(".relative", () => {
30
+ test("should resolve to a relative path (same folder)", () => {
31
+ expect(location_1.default.relative("links/", "links/test.md")).toBe("test.md");
32
+ });
33
+ test("should resolve to a relative path (parent folder)", () => {
34
+ expect(location_1.default.relative("links/", "test.md")).toBe("../test.md");
35
+ });
36
+ test("should resolve to a relative path (child folder)", () => {
37
+ expect(location_1.default.relative("links/", "links/hello/test.md")).toBe("hello/test.md");
38
+ });
39
+ });
40
+ describe(".flatten", () => {
41
+ test("should remove leading slash", () => {
42
+ expect(location_1.default.flatten("/test.md")).toBe("test.md");
43
+ expect(location_1.default.flatten("/hello/cool.md")).toBe("hello/cool.md");
44
+ });
45
+ test("should remove leading slashes", () => {
46
+ expect(location_1.default.flatten("///test.md")).toBe("test.md");
47
+ });
48
+ test("should not break paths", () => {
49
+ expect(location_1.default.flatten("hello/cool.md")).toBe("hello/cool.md");
50
+ });
51
+ });
52
+ describe(".toAbsolute", () => {
53
+ test("should correctly transform as absolute", () => {
54
+ // @ts-expect-error
55
+ expect(location_1.default.toAbsolute("http://google.fr")).toBe("http://google.fr");
56
+ expect(location_1.default.toAbsolute("test.md", "./", "./")).toBe("test.md");
57
+ expect(location_1.default.toAbsolute("folder/test.md", "./", "./")).toBe("folder/test.md");
58
+ });
59
+ test("should correctly handle windows path", () => {
60
+ expect(location_1.default.toAbsolute("folder\\test.md", "./", "./")).toBe("folder/test.md");
61
+ });
62
+ test("should correctly handle absolute path", () => {
63
+ expect(location_1.default.toAbsolute("/test.md", "./", "./")).toBe("test.md");
64
+ expect(location_1.default.toAbsolute("/test.md", "test", "test")).toBe("../test.md");
65
+ expect(location_1.default.toAbsolute("/sub/test.md", "test", "test")).toBe("../sub/test.md");
66
+ expect(location_1.default.toAbsolute("/test.png", "folder", "")).toBe("test.png");
67
+ });
68
+ test("should correctly handle absolute path (windows)", () => {
69
+ expect(location_1.default.toAbsolute("\\test.png", "folder", "")).toBe("test.png");
70
+ });
71
+ test("should resolve path starting by \"/\" in root directory", () => {
72
+ expect(location_1.default.toAbsolute("/test/hello.md", "./", "./")).toBe("test/hello.md");
73
+ });
74
+ test("should resolve path starting by \"/\" in child directory", () => {
75
+ expect(location_1.default.toAbsolute("/test/hello.md", "./hello", "./")).toBe("test/hello.md");
76
+ });
77
+ test("should resolve path starting by \"/\" in child directory, with same output directory", () => {
78
+ expect(location_1.default.toAbsolute("/test/hello.md", "./hello", "./hello")).toBe("../test/hello.md");
79
+ });
80
+ });
81
+ });
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const path_1 = __importDefault(require("path"));
7
+ const path_2 = __importDefault(require("../path"));
8
+ describe("Paths", () => {
9
+ describe("setExtension", () => {
10
+ test("should correctly change extension of filename", () => {
11
+ expect(path_2.default.setExtension("test.md", ".html")).toBe("test.html");
12
+ expect(path_2.default.setExtension("test.md", ".json")).toBe("test.json");
13
+ });
14
+ test("should correctly change extension of path", () => {
15
+ expect(path_2.default.setExtension("hello/test.md", ".html")).toBe(path_1.default.normalize("hello/test.html"));
16
+ expect(path_2.default.setExtension("hello/test.md", ".json")).toBe(path_1.default.normalize("hello/test.json"));
17
+ });
18
+ });
19
+ });
@@ -0,0 +1,104 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const is_1 = __importDefault(require("is"));
7
+ const child_process_1 = __importDefault(require("child_process"));
8
+ const spawn_cmd_1 = require("spawn-cmd");
9
+ const promise_1 = __importDefault(require("./promise"));
10
+ /**
11
+ Execute a command
12
+
13
+ @param {string} command
14
+ @param {Object} options
15
+ @return {Promise}
16
+ */
17
+ function exec(command, options) {
18
+ const d = promise_1.default.defer();
19
+ const child = child_process_1.default.exec(command, options, (err, stdout, stderr) => {
20
+ if (!err) {
21
+ return d.resolve();
22
+ }
23
+ err.message = stdout.toString("utf8") + stderr.toString("utf8");
24
+ d.reject(err);
25
+ });
26
+ child.stdout.on("data", (data) => {
27
+ d.notify(data);
28
+ });
29
+ child.stderr.on("data", (data) => {
30
+ d.notify(data);
31
+ });
32
+ return d.promise;
33
+ }
34
+ /**
35
+ Spawn an executable
36
+
37
+ @param {string} command
38
+ @param {Array} args
39
+ @param {Object} options
40
+ @return {Promise}
41
+ */
42
+ function spawnCmd(command, args, options) {
43
+ const d = promise_1.default.defer();
44
+ const child = spawn_cmd_1.spawn(command, args, options);
45
+ child.on("error", (error) => {
46
+ return d.reject(error);
47
+ });
48
+ child.stdout.on("data", (data) => {
49
+ d.notify(data);
50
+ });
51
+ child.stderr.on("data", (data) => {
52
+ d.notify(data);
53
+ });
54
+ child.on("close", (code) => {
55
+ if (code === 0) {
56
+ d.resolve();
57
+ }
58
+ else {
59
+ d.reject(new Error(`Error with command "${command}"`));
60
+ }
61
+ });
62
+ return d.promise;
63
+ }
64
+ /**
65
+ Transform an option object to a command line string
66
+
67
+ @param {String|number} value
68
+ @param {string}
69
+ */
70
+ function escapeShellArg(value) {
71
+ if (is_1.default.number(value)) {
72
+ return value;
73
+ }
74
+ value = String(value);
75
+ value = value.replace(/"/g, '\\"');
76
+ return `"${value}"`;
77
+ }
78
+ /**
79
+ Transform a map of options into a command line arguments string
80
+
81
+ @param {Object} options
82
+ @return {string}
83
+ */
84
+ function optionsToShellArgs(options) {
85
+ const result = [];
86
+ for (const key in options) {
87
+ const value = options[key];
88
+ if (value === null || value === undefined || value === false) {
89
+ continue;
90
+ }
91
+ if (typeof value === "boolean") {
92
+ result.push(key);
93
+ }
94
+ else {
95
+ result.push(`${key}=${escapeShellArg(value)}`);
96
+ }
97
+ }
98
+ return result.join(" ");
99
+ }
100
+ exports.default = {
101
+ exec: exec,
102
+ spawn: spawnCmd,
103
+ optionsToShellArgs: optionsToShellArgs,
104
+ };