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.

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