You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

408 lines
11 KiB

  1. 'use strict'
  2. const Promise = require('bluebird')
  3. const path = require('path')
  4. const fs = Promise.promisifyAll(require('fs-extra'))
  5. const _ = require('lodash')
  6. const crypto = require('crypto')
  7. /**
  8. * Entries Model
  9. */
  10. module.exports = {
  11. _repoPath: 'repo',
  12. _cachePath: 'data/cache',
  13. /**
  14. * Initialize Entries model
  15. *
  16. * @return {Object} Entries model instance
  17. */
  18. init () {
  19. let self = this
  20. self._repoPath = path.resolve(ROOTPATH, appconfig.paths.repo)
  21. self._cachePath = path.resolve(ROOTPATH, appconfig.paths.data, 'cache')
  22. return self
  23. },
  24. /**
  25. * Check if a document already exists
  26. *
  27. * @param {String} entryPath The entry path
  28. * @return {Promise<Boolean>} True if exists, false otherwise
  29. */
  30. exists (entryPath) {
  31. let self = this
  32. return self.fetchOriginal(entryPath, {
  33. parseMarkdown: false,
  34. parseMeta: false,
  35. parseTree: false,
  36. includeMarkdown: false,
  37. includeParentInfo: false,
  38. cache: false
  39. }).then(() => {
  40. return true
  41. }).catch((err) => { // eslint-disable-line handle-callback-err
  42. return false
  43. })
  44. },
  45. /**
  46. * Fetch a document from cache, otherwise the original
  47. *
  48. * @param {String} entryPath The entry path
  49. * @return {Promise<Object>} Page Data
  50. */
  51. fetch (entryPath) {
  52. let self = this
  53. let cpath = self.getCachePath(entryPath)
  54. return fs.statAsync(cpath).then((st) => {
  55. return st.isFile()
  56. }).catch((err) => { // eslint-disable-line handle-callback-err
  57. return false
  58. }).then((isCache) => {
  59. if (isCache) {
  60. // Load from cache
  61. return fs.readFileAsync(cpath).then((contents) => {
  62. return JSON.parse(contents)
  63. }).catch((err) => { // eslint-disable-line handle-callback-err
  64. winston.error('Corrupted cache file. Deleting it...')
  65. fs.unlinkSync(cpath)
  66. return false
  67. })
  68. } else {
  69. // Load original
  70. return self.fetchOriginal(entryPath)
  71. }
  72. })
  73. },
  74. /**
  75. * Fetches the original document entry
  76. *
  77. * @param {String} entryPath The entry path
  78. * @param {Object} options The options
  79. * @return {Promise<Object>} Page data
  80. */
  81. fetchOriginal (entryPath, options) {
  82. let self = this
  83. let fpath = self.getFullPath(entryPath)
  84. let cpath = self.getCachePath(entryPath)
  85. options = _.defaults(options, {
  86. parseMarkdown: true,
  87. parseMeta: true,
  88. parseTree: true,
  89. includeMarkdown: false,
  90. includeParentInfo: true,
  91. cache: true
  92. })
  93. return fs.statAsync(fpath).then((st) => {
  94. if (st.isFile()) {
  95. return fs.readFileAsync(fpath, 'utf8').then((contents) => {
  96. // Parse contents
  97. let pageData = {
  98. markdown: (options.includeMarkdown) ? contents : '',
  99. html: (options.parseMarkdown) ? mark.parseContent(contents) : '',
  100. meta: (options.parseMeta) ? mark.parseMeta(contents) : {},
  101. tree: (options.parseTree) ? mark.parseTree(contents) : []
  102. }
  103. if (!pageData.meta.title) {
  104. pageData.meta.title = _.startCase(entryPath)
  105. }
  106. pageData.meta.path = entryPath
  107. // Get parent
  108. let parentPromise = (options.includeParentInfo) ? self.getParentInfo(entryPath).then((parentData) => {
  109. return (pageData.parent = parentData)
  110. }).catch((err) => { // eslint-disable-line handle-callback-err
  111. return (pageData.parent = false)
  112. }) : Promise.resolve(true)
  113. return parentPromise.then(() => {
  114. // Cache to disk
  115. if (options.cache) {
  116. let cacheData = JSON.stringify(_.pick(pageData, ['html', 'meta', 'tree', 'parent']), false, false, false)
  117. return fs.writeFileAsync(cpath, cacheData).catch((err) => {
  118. winston.error('Unable to write to cache! Performance may be affected.')
  119. winston.error(err)
  120. return true
  121. })
  122. } else {
  123. return true
  124. }
  125. }).return(pageData)
  126. })
  127. } else {
  128. return false
  129. }
  130. }).catch((err) => { // eslint-disable-line handle-callback-err
  131. throw new Promise.OperationalError('Entry ' + entryPath + ' does not exist!')
  132. })
  133. },
  134. /**
  135. * Parse raw url path and make it safe
  136. *
  137. * @param {String} urlPath The url path
  138. * @return {String} Safe entry path
  139. */
  140. parsePath (urlPath) {
  141. let wlist = new RegExp('[^a-z0-9/-]', 'g')
  142. urlPath = _.toLower(urlPath).replace(wlist, '')
  143. if (urlPath === '/') {
  144. urlPath = 'home'
  145. }
  146. let urlParts = _.filter(_.split(urlPath, '/'), (p) => { return !_.isEmpty(p) })
  147. return _.join(urlParts, '/')
  148. },
  149. /**
  150. * Gets the parent information.
  151. *
  152. * @param {String} entryPath The entry path
  153. * @return {Promise<Object|False>} The parent information.
  154. */
  155. getParentInfo (entryPath) {
  156. let self = this
  157. if (_.includes(entryPath, '/')) {
  158. let parentParts = _.initial(_.split(entryPath, '/'))
  159. let parentPath = _.join(parentParts, '/')
  160. let parentFile = _.last(parentParts)
  161. let fpath = self.getFullPath(parentPath)
  162. return fs.statAsync(fpath).then((st) => {
  163. if (st.isFile()) {
  164. return fs.readFileAsync(fpath, 'utf8').then((contents) => {
  165. let pageMeta = mark.parseMeta(contents)
  166. return {
  167. path: parentPath,
  168. title: (pageMeta.title) ? pageMeta.title : _.startCase(parentFile),
  169. subtitle: (pageMeta.subtitle) ? pageMeta.subtitle : false
  170. }
  171. })
  172. } else {
  173. return Promise.reject(new Error('Parent entry is not a valid file.'))
  174. }
  175. })
  176. } else {
  177. return Promise.reject(new Error('Parent entry is root.'))
  178. }
  179. },
  180. /**
  181. * Gets the full original path of a document.
  182. *
  183. * @param {String} entryPath The entry path
  184. * @return {String} The full path.
  185. */
  186. getFullPath (entryPath) {
  187. return path.join(this._repoPath, entryPath + '.md')
  188. },
  189. /**
  190. * Gets the full cache path of a document.
  191. *
  192. * @param {String} entryPath The entry path
  193. * @return {String} The full cache path.
  194. */
  195. getCachePath (entryPath) {
  196. return path.join(this._cachePath, crypto.createHash('md5').update(entryPath).digest('hex') + '.json')
  197. },
  198. /**
  199. * Gets the entry path from full path.
  200. *
  201. * @param {String} fullPath The full path
  202. * @return {String} The entry path
  203. */
  204. getEntryPathFromFullPath (fullPath) {
  205. let absRepoPath = path.resolve(ROOTPATH, this._repoPath)
  206. return _.chain(fullPath).replace(absRepoPath, '').replace('.md', '').replace(new RegExp('\\\\', 'g'), '/').value()
  207. },
  208. /**
  209. * Update an existing document
  210. *
  211. * @param {String} entryPath The entry path
  212. * @param {String} contents The markdown-formatted contents
  213. * @return {Promise<Boolean>} True on success, false on failure
  214. */
  215. update (entryPath, contents) {
  216. let self = this
  217. let fpath = self.getFullPath(entryPath)
  218. return fs.statAsync(fpath).then((st) => {
  219. if (st.isFile()) {
  220. return self.makePersistent(entryPath, contents).then(() => {
  221. return self.updateCache(entryPath).then(entry => {
  222. return search.add(entry)
  223. })
  224. })
  225. } else {
  226. return Promise.reject(new Error('Entry does not exist!'))
  227. }
  228. }).catch((err) => {
  229. winston.error(err)
  230. return Promise.reject(new Error('Failed to save document.'))
  231. })
  232. },
  233. /**
  234. * Update local cache and search index
  235. *
  236. * @param {String} entryPath The entry path
  237. * @return {Promise} Promise of the operation
  238. */
  239. updateCache (entryPath) {
  240. let self = this
  241. return self.fetchOriginal(entryPath, {
  242. parseMarkdown: true,
  243. parseMeta: true,
  244. parseTree: true,
  245. includeMarkdown: true,
  246. includeParentInfo: true,
  247. cache: true
  248. }).catch(err => {
  249. winston.error(err)
  250. return err
  251. }).then((pageData) => {
  252. return {
  253. entryPath,
  254. meta: pageData.meta,
  255. parent: pageData.parent || {},
  256. text: mark.removeMarkdown(pageData.markdown)
  257. }
  258. }).catch(err => {
  259. winston.error(err)
  260. return err
  261. }).then((content) => {
  262. return db.Entry.findOneAndUpdate({
  263. _id: content.entryPath
  264. }, {
  265. _id: content.entryPath,
  266. title: content.meta.title || content.entryPath,
  267. subtitle: content.meta.subtitle || '',
  268. parent: content.parent.title || ''
  269. }, {
  270. new: true,
  271. upsert: true
  272. })
  273. }).catch(err => {
  274. winston.error(err)
  275. return err
  276. })
  277. },
  278. /**
  279. * Create a new document
  280. *
  281. * @param {String} entryPath The entry path
  282. * @param {String} contents The markdown-formatted contents
  283. * @return {Promise<Boolean>} True on success, false on failure
  284. */
  285. create (entryPath, contents) {
  286. let self = this
  287. return self.exists(entryPath).then((docExists) => {
  288. if (!docExists) {
  289. return self.makePersistent(entryPath, contents).then(() => {
  290. return self.updateCache(entryPath).then(entry => {
  291. return search.add(entry)
  292. })
  293. })
  294. } else {
  295. return Promise.reject(new Error('Entry already exists!'))
  296. }
  297. }).catch((err) => {
  298. winston.error(err)
  299. return Promise.reject(new Error('Something went wrong.'))
  300. })
  301. },
  302. /**
  303. * Makes a document persistent to disk and git repository
  304. *
  305. * @param {String} entryPath The entry path
  306. * @param {String} contents The markdown-formatted contents
  307. * @return {Promise<Boolean>} True on success, false on failure
  308. */
  309. makePersistent (entryPath, contents) {
  310. let self = this
  311. let fpath = self.getFullPath(entryPath)
  312. return fs.outputFileAsync(fpath, contents).then(() => {
  313. return git.commitDocument(entryPath)
  314. })
  315. },
  316. /**
  317. * Move a document
  318. *
  319. * @param {String} entryPath The current entry path
  320. * @param {String} newEntryPath The new entry path
  321. * @return {Promise} Promise of the operation
  322. */
  323. move (entryPath, newEntryPath) {
  324. let self = this
  325. if (_.isEmpty(entryPath) || entryPath === 'home') {
  326. return Promise.reject(new Error('Invalid path!'))
  327. }
  328. return git.moveDocument(entryPath, newEntryPath).then(() => {
  329. return git.commitDocument(newEntryPath).then(() => {
  330. // Delete old cache version
  331. let oldEntryCachePath = self.getCachePath(entryPath)
  332. fs.unlinkAsync(oldEntryCachePath).catch((err) => { return true }) // eslint-disable-line handle-callback-err
  333. // Delete old index entry
  334. search.delete(entryPath)
  335. // Create cache for new entry
  336. return self.updateCache(newEntryPath).then(entry => {
  337. return search.add(entry)
  338. })
  339. })
  340. })
  341. },
  342. /**
  343. * Generate a starter page content based on the entry path
  344. *
  345. * @param {String} entryPath The entry path
  346. * @return {Promise<String>} Starter content
  347. */
  348. getStarter (entryPath) {
  349. let formattedTitle = _.startCase(_.last(_.split(entryPath, '/')))
  350. return fs.readFileAsync(path.join(ROOTPATH, 'client/content/create.md'), 'utf8').then((contents) => {
  351. return _.replace(contents, new RegExp('{TITLE}', 'g'), formattedTitle)
  352. })
  353. }
  354. }