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,88 @@
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 typed_1 = __importDefault(require("error/typed"));
8
+ const wrapped_1 = __importDefault(require("error/wrapped"));
9
+ // Enforce as an Error object, and cleanup message
10
+ function enforce(err) {
11
+ if (is_1.default.string(err))
12
+ err = new Error(err);
13
+ err.message = err.message.replace(/^Error: /, "");
14
+ return err;
15
+ }
16
+ // Random error wrappers during parsing/generation
17
+ const ParsingError = wrapped_1.default({
18
+ message: "Parsing Error: {origMessage}",
19
+ type: "parse",
20
+ });
21
+ const OutputError = wrapped_1.default({
22
+ message: "Output Error: {origMessage}",
23
+ type: "generate",
24
+ });
25
+ // A file does not exists
26
+ const FileNotFoundError = typed_1.default({
27
+ type: "file.not-found",
28
+ message: 'No "{filename}" file (or is ignored)',
29
+ filename: null,
30
+ });
31
+ // A file cannot be parsed
32
+ const FileNotParsableError = typed_1.default({
33
+ type: "file.not-parsable",
34
+ message: '"{filename}" file cannot be parsed',
35
+ filename: null,
36
+ });
37
+ // A file is outside the scope
38
+ const FileOutOfScopeError = typed_1.default({
39
+ type: "file.out-of-scope",
40
+ message: '"{filename}" not in "{root}"',
41
+ filename: null,
42
+ root: null,
43
+ code: "EACCESS",
44
+ });
45
+ // A file is outside the scope
46
+ const RequireInstallError = typed_1.default({
47
+ type: "install.required",
48
+ message: '"{cmd}" is not installed.\n{install}',
49
+ cmd: null,
50
+ code: "ENOENT",
51
+ install: "",
52
+ });
53
+ // Error for nunjucks templates
54
+ const TemplateError = wrapped_1.default({
55
+ message: 'Error compiling template "{filename}": {origMessage}',
56
+ type: "template",
57
+ filename: null,
58
+ });
59
+ // Error for nunjucks templates
60
+ const PluginError = wrapped_1.default({
61
+ message: 'Error with plugin "{plugin}": {origMessage}',
62
+ type: "plugin",
63
+ plugin: null,
64
+ });
65
+ // Error with the book's configuration
66
+ const ConfigurationError = wrapped_1.default({
67
+ message: "Error with book's configuration: {origMessage}",
68
+ type: "configuration",
69
+ });
70
+ // Error during ebook generation
71
+ const EbookError = wrapped_1.default({
72
+ message: "Error during ebook generation: {origMessage}\n{stdout}",
73
+ type: "ebook",
74
+ stdout: "",
75
+ });
76
+ exports.default = {
77
+ enforce: enforce,
78
+ ParsingError: ParsingError,
79
+ OutputError: OutputError,
80
+ RequireInstallError: RequireInstallError,
81
+ FileNotParsableError: FileNotParsableError,
82
+ FileNotFoundError: FileNotFoundError,
83
+ FileOutOfScopeError: FileOutOfScopeError,
84
+ TemplateError: TemplateError,
85
+ PluginError: PluginError,
86
+ ConfigurationError: ConfigurationError,
87
+ EbookError: EbookError,
88
+ };
@@ -0,0 +1,163 @@
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 fs_1 = __importDefault(require("fs"));
7
+ const mkdirp_1 = __importDefault(require("mkdirp"));
8
+ const destroy_1 = __importDefault(require("destroy"));
9
+ const tmp_1 = __importDefault(require("tmp"));
10
+ const request_1 = __importDefault(require("request"));
11
+ const path_1 = __importDefault(require("path"));
12
+ const cp_1 = __importDefault(require("cp"));
13
+ const cpr_1 = __importDefault(require("cpr"));
14
+ const promise_1 = __importDefault(require("./promise"));
15
+ // Write a stream to a file
16
+ function writeStream(filename, st) {
17
+ const d = promise_1.default.defer();
18
+ const wstream = fs_1.default.createWriteStream(filename);
19
+ const cleanup = function () {
20
+ destroy_1.default(wstream);
21
+ wstream.removeAllListeners();
22
+ };
23
+ wstream.on("finish", () => {
24
+ cleanup();
25
+ d.resolve();
26
+ });
27
+ wstream.on("error", (err) => {
28
+ cleanup();
29
+ d.reject(err);
30
+ });
31
+ st.on("error", (err) => {
32
+ cleanup();
33
+ d.reject(err);
34
+ });
35
+ st.pipe(wstream);
36
+ return d.promise;
37
+ }
38
+ // Return a promise resolved with a boolean
39
+ function fileExists(filename) {
40
+ const d = promise_1.default.defer();
41
+ fs_1.default.stat(filename, (error) => {
42
+ if (error) {
43
+ d.resolve(false);
44
+ }
45
+ else {
46
+ d.resolve(true);
47
+ }
48
+ });
49
+ return d.promise;
50
+ }
51
+ // Generate temporary file
52
+ function genTmpFile(opts) {
53
+ return promise_1.default.nfcall(tmp_1.default.file, opts).get(0);
54
+ }
55
+ // Generate temporary dir
56
+ function genTmpDir(opts) {
57
+ return promise_1.default.nfcall(tmp_1.default.dir, opts).get(0);
58
+ }
59
+ // Download an image
60
+ function download(uri, dest) {
61
+ return writeStream(dest, request_1.default(uri));
62
+ }
63
+ // Find a filename available in a folder
64
+ function uniqueFilename(base, filename) {
65
+ const ext = path_1.default.extname(filename);
66
+ filename = path_1.default.resolve(base, filename);
67
+ filename = path_1.default.join(path_1.default.dirname(filename), path_1.default.basename(filename, ext));
68
+ let _filename = filename + ext;
69
+ let i = 0;
70
+ while (fs_1.default.existsSync(filename)) {
71
+ _filename = `${filename}_${i}${ext}`;
72
+ i = i + 1;
73
+ }
74
+ return promise_1.default(path_1.default.relative(base, _filename));
75
+ }
76
+ // Create all required folder to create a file
77
+ function ensureFile(filename) {
78
+ const base = path_1.default.dirname(filename);
79
+ return promise_1.default(mkdirp_1.default(base));
80
+ }
81
+ // Remove a folder
82
+ function rmDir(base) {
83
+ return promise_1.default.nfcall(fs_1.default.rmdir, base, {
84
+ recursive: true,
85
+ });
86
+ }
87
+ /**
88
+ Assert a file, if it doesn't exist, call "generator"
89
+
90
+ @param {string} filePath
91
+ @param {Function} generator
92
+ @return {Promise}
93
+ */
94
+ function assertFile(filePath, generator) {
95
+ return fileExists(filePath).then((exists) => {
96
+ if (exists)
97
+ return;
98
+ return generator();
99
+ });
100
+ }
101
+ /**
102
+ Pick a file, returns the absolute path if exists, undefined otherwise
103
+
104
+ @param {string} rootFolder
105
+ @param {string} fileName
106
+ @return {string}
107
+ */
108
+ function pickFile(rootFolder, fileName) {
109
+ const result = path_1.default.join(rootFolder, fileName);
110
+ if (fs_1.default.existsSync(result)) {
111
+ return result;
112
+ }
113
+ return undefined;
114
+ }
115
+ /**
116
+ Ensure that a directory exists and is empty
117
+
118
+ @param {string} folder
119
+ @return {Promise}
120
+ */
121
+ function ensureFolder(rootFolder) {
122
+ return rmDir(rootFolder)
123
+ .fail(() => {
124
+ return promise_1.default();
125
+ })
126
+ .then(() => {
127
+ return promise_1.default(mkdirp_1.default(rootFolder));
128
+ });
129
+ }
130
+ exports.default = {
131
+ exists: fileExists,
132
+ existsSync: fs_1.default.existsSync,
133
+ mkdirp: mkdirp_1.default,
134
+ readFile: promise_1.default.nfbind(fs_1.default.readFile),
135
+ writeFile: (filePath, content) => {
136
+ const d = promise_1.default.defer();
137
+ fs_1.default.writeFile(filePath, content, (error) => {
138
+ if (error) {
139
+ d.reject(error);
140
+ }
141
+ else {
142
+ d.resolve();
143
+ }
144
+ });
145
+ return d.promise;
146
+ },
147
+ assertFile: assertFile,
148
+ pickFile: pickFile,
149
+ stat: promise_1.default.nfbind(fs_1.default.lstat),
150
+ statSync: fs_1.default.lstatSync,
151
+ readdir: promise_1.default.nfbind(fs_1.default.readdir),
152
+ writeStream: writeStream,
153
+ readStream: fs_1.default.createReadStream,
154
+ copy: promise_1.default.nfbind(cp_1.default),
155
+ copyDir: promise_1.default.nfbind(cpr_1.default),
156
+ tmpFile: genTmpFile,
157
+ tmpDir: genTmpDir,
158
+ download: download,
159
+ uniqueFilename: uniqueFilename,
160
+ ensureFile: ensureFile,
161
+ ensureFolder: ensureFolder,
162
+ rmDir: rmDir,
163
+ };
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ let lastKey = 0;
4
+ /*
5
+ Generate a random key
6
+ @return {string}
7
+ */
8
+ function generateKey() {
9
+ lastKey += 1;
10
+ const str = lastKey.toString(16);
11
+ return "00000".slice(str.length) + str;
12
+ }
13
+ exports.default = generateKey;
@@ -0,0 +1,116 @@
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 path_1 = __importDefault(require("path"));
8
+ const crc_1 = __importDefault(require("crc"));
9
+ const urijs_1 = __importDefault(require("urijs"));
10
+ const path_2 = __importDefault(require("./path"));
11
+ const promise_1 = __importDefault(require("./promise"));
12
+ const command_1 = __importDefault(require("./command"));
13
+ const fs_1 = __importDefault(require("./fs"));
14
+ const GIT_PREFIX = "git+";
15
+ function Git() {
16
+ this.tmpDir;
17
+ this.cloned = {};
18
+ }
19
+ // Return an unique ID for a combinaison host/ref
20
+ Git.prototype.repoID = function (host, ref) {
21
+ return crc_1.default.crc32(`${host}#${ref || ""}`).toString(16);
22
+ };
23
+ // Allocate a temporary folder for cloning repos in it
24
+ Git.prototype.allocateDir = function () {
25
+ const that = this;
26
+ if (this.tmpDir)
27
+ return promise_1.default();
28
+ // @ts-expect-error ts-migrate(2554) FIXME: Expected 1 arguments, but got 0.
29
+ return fs_1.default.tmpDir().then((dir) => {
30
+ that.tmpDir = dir;
31
+ });
32
+ };
33
+ // Clone a git repository if non existant
34
+ Git.prototype.clone = function (host, ref) {
35
+ const that = this;
36
+ return (this.allocateDir()
37
+ // Return or clone the git repo
38
+ .then(() => {
39
+ // Unique ID for repo/ref combinaison
40
+ const repoId = that.repoID(host, ref);
41
+ // Absolute path to the folder
42
+ const repoPath = path_1.default.join(that.tmpDir, repoId);
43
+ if (that.cloned[repoId])
44
+ return repoPath;
45
+ // Clone repo
46
+ return (command_1.default
47
+ // @ts-expect-error ts-migrate(2554) FIXME: Expected 2 arguments, but got 1.
48
+ .exec(`git clone ${host} ${repoPath}`)
49
+ // Checkout reference if specified
50
+ .then(() => {
51
+ that.cloned[repoId] = true;
52
+ if (!ref)
53
+ return;
54
+ return command_1.default.exec(`git checkout ${ref}`, { cwd: repoPath });
55
+ })
56
+ .thenResolve(repoPath));
57
+ }));
58
+ };
59
+ // Get file from a git repo
60
+ Git.prototype.resolve = function (giturl) {
61
+ // Path to a file in a git repo?
62
+ if (!Git.isUrl(giturl)) {
63
+ if (this.resolveRoot(giturl))
64
+ return promise_1.default(giturl);
65
+ return promise_1.default(null);
66
+ }
67
+ if (is_1.default.string(giturl))
68
+ giturl = Git.parseUrl(giturl);
69
+ if (!giturl)
70
+ return promise_1.default(null);
71
+ // Clone or get from cache
72
+ return this.clone(giturl.host, giturl.ref).then((repo) => {
73
+ return path_1.default.resolve(repo, giturl.filepath);
74
+ });
75
+ };
76
+ // Return root of git repo from a filepath
77
+ Git.prototype.resolveRoot = function (filepath) {
78
+ // No git repo cloned, or file is not in a git repository
79
+ if (!this.tmpDir || !path_2.default.isInRoot(this.tmpDir, filepath))
80
+ return null;
81
+ // Extract first directory (is the repo id)
82
+ const relativeToGit = path_1.default.relative(this.tmpDir, filepath);
83
+ const repoId = relativeToGit.split(path_1.default.sep)[0];
84
+ if (!repoId) {
85
+ return;
86
+ }
87
+ // Return an absolute file
88
+ return path_1.default.resolve(this.tmpDir, repoId);
89
+ };
90
+ // Check if an url is a git dependency url
91
+ Git.isUrl = function (giturl) {
92
+ return giturl.indexOf(GIT_PREFIX) === 0;
93
+ };
94
+ // Parse and extract infos
95
+ Git.parseUrl = function (giturl) {
96
+ if (!Git.isUrl(giturl))
97
+ return null;
98
+ giturl = giturl.slice(GIT_PREFIX.length);
99
+ const uri = new urijs_1.default(giturl);
100
+ const ref = uri.fragment() || null;
101
+ uri.fragment(null);
102
+ // Extract file inside the repo (after the .git)
103
+ const fileParts = uri.path().split(".git");
104
+ let filepath = fileParts.length > 1 ? fileParts.slice(1).join(".git") : "";
105
+ if (filepath[0] == "/") {
106
+ filepath = filepath.slice(1);
107
+ }
108
+ // Recreate pathname without the real filename
109
+ uri.path(`${fileParts[0]}.git`);
110
+ return {
111
+ host: uri.toString(),
112
+ ref: ref,
113
+ filepath: filepath,
114
+ };
115
+ };
116
+ exports.default = Git;
@@ -0,0 +1,22 @@
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("./promise"));
7
+ const fs_1 = __importDefault(require("./fs"));
8
+ // Converts a inline data: to png file
9
+ function convertInlinePNG(source, dest) {
10
+ if (!/^data:image\/png/.test(source))
11
+ return promise_1.default.reject(new Error("Source is not a PNG data-uri"));
12
+ const base64data = source.split("data:image/png;base64,")[1];
13
+ const buf = Buffer.from(base64data, "base64");
14
+ return fs_1.default.writeFile(dest, buf).then(() => {
15
+ if (fs_1.default.existsSync(dest))
16
+ return;
17
+ throw new Error(`Error converting ${source} into ${dest}`);
18
+ });
19
+ }
20
+ exports.default = {
21
+ convertInlinePNG: convertInlinePNG,
22
+ };
@@ -0,0 +1,129 @@
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 url_1 = __importDefault(require("url"));
7
+ const path_1 = __importDefault(require("path"));
8
+ // Is the url an external url
9
+ function isExternal(href) {
10
+ try {
11
+ return Boolean(url_1.default.parse(href).protocol) && !isDataURI(href);
12
+ }
13
+ catch (err) {
14
+ return false;
15
+ }
16
+ }
17
+ // Is the url an iniline data-uri
18
+ function isDataURI(href) {
19
+ try {
20
+ return Boolean(url_1.default.parse(href).protocol) && url_1.default.parse(href).protocol === "data:";
21
+ }
22
+ catch (err) {
23
+ return false;
24
+ }
25
+ }
26
+ // Inverse of isExternal
27
+ function isRelative(href) {
28
+ return !isExternal(href);
29
+ }
30
+ // Return true if the link is an achor
31
+ function isAnchor(href) {
32
+ try {
33
+ const parsed = url_1.default.parse(href);
34
+ return !!(!parsed.protocol && !parsed.path && parsed.hash);
35
+ }
36
+ catch (err) {
37
+ return false;
38
+ }
39
+ }
40
+ // Normalize a path to be a link
41
+ function normalize(s) {
42
+ return path_1.default.normalize(s).replace(/\\/g, "/");
43
+ }
44
+ /**
45
+ * Flatten a path, it removes the leading "/"
46
+ *
47
+ * @param {string} href
48
+ * @return {string}
49
+ */
50
+ function flatten(href) {
51
+ href = normalize(href);
52
+ if (href[0] == "/") {
53
+ href = normalize(href.slice(1));
54
+ }
55
+ return href;
56
+ }
57
+ /**
58
+ * Convert a relative path to absolute
59
+ *
60
+ * @param {string} _href
61
+ * @param {string} dir: directory parent of the file currently in rendering process
62
+ * @param {string} outdir: directory parent from the html output
63
+ * @return {string}
64
+ */
65
+ function toAbsolute(_href, dir, outdir) {
66
+ if (isExternal(_href) || isDataURI(_href)) {
67
+ return _href;
68
+ }
69
+ outdir = outdir == undefined ? dir : outdir;
70
+ _href = normalize(_href);
71
+ dir = normalize(dir);
72
+ outdir = normalize(outdir);
73
+ // Path "_href" inside the base folder
74
+ let hrefInRoot = normalize(path_1.default.join(dir, _href));
75
+ if (_href[0] == "/") {
76
+ hrefInRoot = normalize(_href.slice(1));
77
+ }
78
+ // Make it relative to output
79
+ _href = path_1.default.relative(outdir, hrefInRoot);
80
+ // Normalize windows paths
81
+ _href = normalize(_href);
82
+ return _href;
83
+ }
84
+ /**
85
+ * Convert an absolute path to a relative path for a specific folder (dir)
86
+ * ('test/', 'hello.md') -> '../hello.md'
87
+ *
88
+ * @param {string} dir: current directory
89
+ * @param {string} file: absolute path of file
90
+ * @return {string}
91
+ */
92
+ function relative(dir, file) {
93
+ const isDirectory = file.slice(-1) === "/";
94
+ return normalize(path_1.default.relative(dir, file)) + (isDirectory ? "/" : "");
95
+ }
96
+ /**
97
+ * Convert an absolute path to a relative path for a specific folder (dir)
98
+ * ('test/test.md', 'hello.md') -> '../hello.md'
99
+ *
100
+ * @param {string} baseFile: current file
101
+ * @param {string} file: absolute path of file
102
+ * @return {string}
103
+ */
104
+ function relativeForFile(baseFile, file) {
105
+ return relative(path_1.default.dirname(baseFile), file);
106
+ }
107
+ /**
108
+ * Compare two paths, return true if they are identical
109
+ * ('README.md', './README.md') -> true
110
+ *
111
+ * @param {string} p1: first path
112
+ * @param {string} p2: second path
113
+ * @return {boolean}
114
+ */
115
+ function areIdenticalPaths(p1, p2) {
116
+ return normalize(p1) === normalize(p2);
117
+ }
118
+ exports.default = {
119
+ areIdenticalPaths: areIdenticalPaths,
120
+ isDataURI: isDataURI,
121
+ isExternal: isExternal,
122
+ isRelative: isRelative,
123
+ isAnchor: isAnchor,
124
+ normalize: normalize,
125
+ toAbsolute: toAbsolute,
126
+ relative: relative,
127
+ relativeForFile: relativeForFile,
128
+ flatten: flatten,
129
+ };