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.

60 lines
1.6 KiB

  1. 'use strict'
  2. const crypto = require('crypto')
  3. const path = require('path')
  4. const qs = require('querystring')
  5. const _ = require('lodash')
  6. module.exports = {
  7. /**
  8. * Parse raw url path and make it safe
  9. *
  10. * @param {String} urlPath The url path
  11. * @return {String} Safe entry path
  12. */
  13. parsePath (urlPath) {
  14. urlPath = qs.unescape(urlPath)
  15. let wlist = new RegExp('(?!([^a-z0-9]|' + appdata.regex.cjk.source + '|[/-]))', 'g')
  16. urlPath = _.toLower(urlPath).replace(wlist, '')
  17. if (urlPath === '/') {
  18. urlPath = 'home'
  19. }
  20. let urlParts = _.filter(_.split(urlPath, '/'), (p) => { return !_.isEmpty(p) })
  21. return _.join(urlParts, '/')
  22. },
  23. /**
  24. * Gets the full original path of a document.
  25. *
  26. * @param {String} entryPath The entry path
  27. * @return {String} The full path.
  28. */
  29. getFullPath (entryPath) {
  30. return path.join(appdata.repoPath, entryPath + '.md')
  31. },
  32. /**
  33. * Gets the full cache path of a document.
  34. *
  35. * @param {String} entryPath The entry path
  36. * @return {String} The full cache path.
  37. */
  38. getCachePath (entryPath) {
  39. return path.join(appdata.cachePath, crypto.createHash('md5').update(entryPath).digest('hex') + '.json')
  40. },
  41. /**
  42. * Gets the entry path from full path.
  43. *
  44. * @param {String} fullPath The full path
  45. * @return {String} The entry path
  46. */
  47. getEntryPathFromFullPath (fullPath) {
  48. let absRepoPath = path.resolve(ROOTPATH, appdata.repoPath)
  49. return _.chain(fullPath).replace(absRepoPath, '').replace('.md', '').replace(new RegExp('\\\\', 'g'), '/').value()
  50. }
  51. }