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.

455 lines
12 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. return Promise.reject(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. }).then((pageData) => {
  249. return {
  250. entryPath,
  251. meta: pageData.meta,
  252. parent: pageData.parent || {},
  253. text: mark.removeMarkdown(pageData.markdown)
  254. }
  255. }).then((content) => {
  256. return db.Entry.findOneAndUpdate({
  257. _id: content.entryPath
  258. }, {
  259. _id: content.entryPath,
  260. title: content.meta.title || content.entryPath,
  261. subtitle: content.meta.subtitle || '',
  262. parent: content.parent.title || '',
  263. content: content.text || ''
  264. }, {
  265. new: true,
  266. upsert: true
  267. })
  268. })
  269. },
  270. /**
  271. * Create a new document
  272. *
  273. * @param {String} entryPath The entry path
  274. * @param {String} contents The markdown-formatted contents
  275. * @return {Promise<Boolean>} True on success, false on failure
  276. */
  277. create (entryPath, contents) {
  278. let self = this
  279. return self.exists(entryPath).then((docExists) => {
  280. if (!docExists) {
  281. return self.makePersistent(entryPath, contents).then(() => {
  282. return self.updateCache(entryPath).then(entry => {
  283. return search.add(entry)
  284. })
  285. })
  286. } else {
  287. return Promise.reject(new Error('Entry already exists!'))
  288. }
  289. }).catch((err) => {
  290. winston.error(err)
  291. return Promise.reject(new Error('Something went wrong.'))
  292. })
  293. },
  294. /**
  295. * Makes a document persistent to disk and git repository
  296. *
  297. * @param {String} entryPath The entry path
  298. * @param {String} contents The markdown-formatted contents
  299. * @return {Promise<Boolean>} True on success, false on failure
  300. */
  301. makePersistent (entryPath, contents) {
  302. let self = this
  303. let fpath = self.getFullPath(entryPath)
  304. return fs.outputFileAsync(fpath, contents).then(() => {
  305. return git.commitDocument(entryPath)
  306. })
  307. },
  308. /**
  309. * Move a document
  310. *
  311. * @param {String} entryPath The current entry path
  312. * @param {String} newEntryPath The new entry path
  313. * @return {Promise} Promise of the operation
  314. */
  315. move (entryPath, newEntryPath) {
  316. let self = this
  317. if (_.isEmpty(entryPath) || entryPath === 'home') {
  318. return Promise.reject(new Error('Invalid path!'))
  319. }
  320. return git.moveDocument(entryPath, newEntryPath).then(() => {
  321. return git.commitDocument(newEntryPath).then(() => {
  322. // Delete old cache version
  323. let oldEntryCachePath = self.getCachePath(entryPath)
  324. fs.unlinkAsync(oldEntryCachePath).catch((err) => { return true }) // eslint-disable-line handle-callback-err
  325. // Delete old index entry
  326. search.delete(entryPath)
  327. // Create cache for new entry
  328. return self.updateCache(newEntryPath).then(entry => {
  329. return search.add(entry)
  330. })
  331. })
  332. })
  333. },
  334. /**
  335. * Generate a starter page content based on the entry path
  336. *
  337. * @param {String} entryPath The entry path
  338. * @return {Promise<String>} Starter content
  339. */
  340. getStarter (entryPath) {
  341. let formattedTitle = _.startCase(_.last(_.split(entryPath, '/')))
  342. return fs.readFileAsync(path.join(ROOTPATH, 'client/content/create.md'), 'utf8').then((contents) => {
  343. return _.replace(contents, new RegExp('{TITLE}', 'g'), formattedTitle)
  344. })
  345. },
  346. /**
  347. * Searches entries based on terms.
  348. *
  349. * @param {String} terms The terms to search for
  350. * @return {Promise<Object>} Promise of the search results
  351. */
  352. search (terms) {
  353. terms = _.chain(terms)
  354. .deburr()
  355. .toLower()
  356. .trim()
  357. .replace(/[^a-z0-9\- ]/g, '')
  358. .split(' ')
  359. .filter((f) => { return !_.isEmpty(f) })
  360. .join(' ')
  361. .value()
  362. return db.Entry.find(
  363. { $text: { $search: terms } },
  364. { score: { $meta: 'textScore' }, title: 1 }
  365. )
  366. .sort({ score: { $meta: 'textScore' } })
  367. .limit(10)
  368. .exec()
  369. .then((hits) => {
  370. if (hits.length < 5) {
  371. let regMatch = new RegExp('^' + _.split(terms, ' ')[0])
  372. return db.Entry.find({
  373. _id: { $regex: regMatch }
  374. }, '_id')
  375. .sort('_id')
  376. .limit(5)
  377. .exec()
  378. .then((matches) => {
  379. return {
  380. match: hits,
  381. suggest: (matches) ? _.map(matches, '_id') : []
  382. }
  383. })
  384. } else {
  385. return {
  386. match: _.filter(hits, (h) => { return h._doc.score >= 1 }),
  387. suggest: []
  388. }
  389. }
  390. }).catch((err) => {
  391. winston.error(err)
  392. return {
  393. match: [],
  394. suggest: []
  395. }
  396. })
  397. }
  398. }