pages.js 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. const Model = require('objection').Model
  2. const _ = require('lodash')
  3. const JSBinType = require('js-binary').Type
  4. const pageHelper = require('../helpers/page')
  5. const path = require('path')
  6. const fs = require('fs-extra')
  7. const yaml = require('js-yaml')
  8. const striptags = require('striptags')
  9. const emojiRegex = require('emoji-regex')
  10. const he = require('he')
  11. const CleanCSS = require('clean-css')
  12. const TurndownService = require('turndown')
  13. const turndownPluginGfm = require('@joplin/turndown-plugin-gfm').gfm
  14. const cheerio = require('cheerio')
  15. const pageRegex = /^[a-zA0-90-9-_/]*$/
  16. const frontmatterRegex = {
  17. html: /^(<!-{2}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{2}>)?(?:\n|\r)*([\w\W]*)*/,
  18. legacy: /^(<!-- TITLE: ?([\w\W]+?) ?-{2}>)?(?:\n|\r)?(<!-- SUBTITLE: ?([\w\W]+?) ?-{2}>)?(?:\n|\r)*([\w\W]*)*/i,
  19. markdown: /^(-{3}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{3})?(?:\n|\r)*([\w\W]*)*/
  20. }
  21. const punctuationRegex = /[!,:;/\\_+\-=()&#@<>$~%^*[\]{}"'|]+|(\.\s)|(\s\.)/ig
  22. // const htmlEntitiesRegex = /(&#[0-9]{3};)|(&#x[a-zA-Z0-9]{2};)/ig
  23. /**
  24. * Pages model
  25. */
  26. module.exports = class Page extends Model {
  27. static get tableName() { return 'pages' }
  28. static get jsonSchema () {
  29. return {
  30. type: 'object',
  31. required: ['path', 'title'],
  32. properties: {
  33. id: {type: 'string'},
  34. path: {type: 'string'},
  35. hash: {type: 'string'},
  36. title: {type: 'string'},
  37. description: {type: 'string'},
  38. publishState: {type: 'string'},
  39. publishStartDate: {type: 'string'},
  40. publishEndDate: {type: 'string'},
  41. content: {type: 'string'},
  42. contentType: {type: 'string'},
  43. siteId: {type: 'string'},
  44. createdAt: {type: 'string'},
  45. updatedAt: {type: 'string'}
  46. }
  47. }
  48. }
  49. static get jsonAttributes() {
  50. return ['config', 'historyData', 'relations', 'scripts', 'toc']
  51. }
  52. static get relationMappings() {
  53. return {
  54. tags: {
  55. relation: Model.ManyToManyRelation,
  56. modelClass: require('./tags'),
  57. join: {
  58. from: 'pages.id',
  59. through: {
  60. from: 'pageTags.pageId',
  61. to: 'pageTags.tagId'
  62. },
  63. to: 'tags.id'
  64. }
  65. },
  66. links: {
  67. relation: Model.HasManyRelation,
  68. modelClass: require('./pageLinks'),
  69. join: {
  70. from: 'pages.id',
  71. to: 'pageLinks.pageId'
  72. }
  73. },
  74. author: {
  75. relation: Model.BelongsToOneRelation,
  76. modelClass: require('./users'),
  77. join: {
  78. from: 'pages.authorId',
  79. to: 'users.id'
  80. }
  81. },
  82. creator: {
  83. relation: Model.BelongsToOneRelation,
  84. modelClass: require('./users'),
  85. join: {
  86. from: 'pages.creatorId',
  87. to: 'users.id'
  88. }
  89. },
  90. locale: {
  91. relation: Model.BelongsToOneRelation,
  92. modelClass: require('./locales'),
  93. join: {
  94. from: 'pages.localeCode',
  95. to: 'locales.code'
  96. }
  97. }
  98. }
  99. }
  100. $beforeUpdate() {
  101. this.updatedAt = new Date().toISOString()
  102. }
  103. $beforeInsert() {
  104. this.createdAt = new Date().toISOString()
  105. this.updatedAt = new Date().toISOString()
  106. }
  107. /**
  108. * Solving the violates foreign key constraint using cascade strategy
  109. * using static hooks
  110. * @see https://vincit.github.io/objection.js/api/types/#type-statichookarguments
  111. */
  112. static async beforeDelete({ asFindQuery }) {
  113. const page = await asFindQuery().select('id')
  114. await WIKI.db.comments.query().delete().where('pageId', page[0].id)
  115. }
  116. /**
  117. * Cache Schema
  118. */
  119. static get cacheSchema() {
  120. return new JSBinType({
  121. id: 'string',
  122. authorId: 'string',
  123. authorName: 'string',
  124. createdAt: 'string',
  125. creatorId: 'string',
  126. creatorName: 'string',
  127. description: 'string',
  128. editor: 'string',
  129. publishState: 'string',
  130. publishEndDate: 'string',
  131. publishStartDate: 'string',
  132. render: 'string',
  133. siteId: 'string',
  134. tags: [
  135. {
  136. tag: 'string'
  137. }
  138. ],
  139. extra: {
  140. js: 'string',
  141. css: 'string'
  142. },
  143. title: 'string',
  144. toc: 'string',
  145. updatedAt: 'string'
  146. })
  147. }
  148. /**
  149. * Inject page metadata into contents
  150. *
  151. * @returns {string} Page Contents with Injected Metadata
  152. */
  153. injectMetadata () {
  154. return pageHelper.injectPageMetadata(this)
  155. }
  156. /**
  157. * Get the page's file extension based on content type
  158. *
  159. * @returns {string} File Extension
  160. */
  161. getFileExtension() {
  162. return pageHelper.getFileExtension(this.contentType)
  163. }
  164. /**
  165. * Parse injected page metadata from raw content
  166. *
  167. * @param {String} raw Raw file contents
  168. * @param {String} contentType Content Type
  169. * @returns {Object} Parsed Page Metadata with Raw Content
  170. */
  171. static parseMetadata (raw, contentType) {
  172. let result
  173. try {
  174. switch (contentType) {
  175. case 'markdown':
  176. result = frontmatterRegex.markdown.exec(raw)
  177. if (result[2]) {
  178. return {
  179. ...yaml.safeLoad(result[2]),
  180. content: result[3]
  181. }
  182. } else {
  183. // Attempt legacy v1 format
  184. result = frontmatterRegex.legacy.exec(raw)
  185. if (result[2]) {
  186. return {
  187. title: result[2],
  188. description: result[4],
  189. content: result[5]
  190. }
  191. }
  192. }
  193. break
  194. case 'html':
  195. result = frontmatterRegex.html.exec(raw)
  196. if (result[2]) {
  197. return {
  198. ...yaml.safeLoad(result[2]),
  199. content: result[3]
  200. }
  201. }
  202. break
  203. }
  204. } catch (err) {
  205. WIKI.logger.warn('Failed to parse page metadata. Invalid syntax.')
  206. }
  207. return {
  208. content: raw
  209. }
  210. }
  211. /**
  212. * Create a New Page
  213. *
  214. * @param {Object} opts Page Properties
  215. * @returns {Promise} Promise of the Page Model Instance
  216. */
  217. static async createPage(opts) {
  218. // -> Validate site
  219. if (!WIKI.sites[opts.siteId]) {
  220. throw new WIKI.Error.Custom('InvalidSiteId', 'Site ID is invalid.')
  221. }
  222. // -> Remove trailing slash
  223. if (opts.path.endsWith('/')) {
  224. opts.path = opts.path.slice(0, -1)
  225. }
  226. // -> Remove starting slash
  227. if (opts.path.startsWith('/')) {
  228. opts.path = opts.path.slice(1)
  229. }
  230. // -> Validate path
  231. if (!pageRegex.test(opts.path)) {
  232. throw new Error('ERR_INVALID_PATH')
  233. }
  234. opts.path = opts.path.toLowerCase()
  235. const dotPath = opts.path.replaceAll('/', '.').replaceAll('-', '_')
  236. // -> Check for page access
  237. if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
  238. locale: opts.locale,
  239. path: opts.path
  240. })) {
  241. throw new WIKI.Error.PageDeleteForbidden()
  242. }
  243. // -> Check for duplicate
  244. const dupCheck = await WIKI.db.pages.query().select('id').where('localeCode', opts.locale).where('path', opts.path).first()
  245. if (dupCheck) {
  246. throw new WIKI.Error.PageDuplicateCreate()
  247. }
  248. // -> Check for empty content
  249. if (!opts.content || _.trim(opts.content).length < 1) {
  250. throw new WIKI.Error.PageEmptyContent()
  251. }
  252. // -> Format CSS Scripts
  253. let scriptCss = ''
  254. if (WIKI.auth.checkAccess(opts.user, ['write:styles'], {
  255. locale: opts.locale,
  256. path: opts.path
  257. })) {
  258. if (!_.isEmpty(opts.scriptCss)) {
  259. scriptCss = new CleanCSS({ inline: false }).minify(opts.scriptCss).styles
  260. } else {
  261. scriptCss = ''
  262. }
  263. }
  264. // -> Format JS Scripts
  265. let scriptJsLoad = ''
  266. let scriptJsUnload = ''
  267. if (WIKI.auth.checkAccess(opts.user, ['write:scripts'], {
  268. locale: opts.locale,
  269. path: opts.path
  270. })) {
  271. scriptJsLoad = opts.scriptJsLoad || ''
  272. scriptJsUnload = opts.scriptJsUnload || ''
  273. }
  274. // -> Create page
  275. const page = await WIKI.db.pages.query().insert({
  276. authorId: opts.user.id,
  277. content: opts.content,
  278. creatorId: opts.user.id,
  279. config: {
  280. allowComments: opts.allowComments ?? true,
  281. allowContributions: opts.allowContributions ?? true,
  282. allowRatings: opts.allowRatings ?? true,
  283. showSidebar: opts.showSidebar ?? true,
  284. showTags: opts.showTags ?? true,
  285. showToc: opts.showToc ?? true,
  286. tocDepth: opts.tocDepth ?? WIKI.sites[opts.siteId].config?.defaults.tocDepth
  287. },
  288. contentType: WIKI.data.editors[opts.editor]?.contentType ?? 'text',
  289. description: opts.description,
  290. dotPath: dotPath,
  291. editor: opts.editor,
  292. hash: pageHelper.generateHash({ path: opts.path, locale: opts.locale }),
  293. icon: opts.icon,
  294. isBrowsable: opts.isBrowsable ?? true,
  295. localeCode: opts.locale,
  296. path: opts.path,
  297. publishState: opts.publishState,
  298. publishEndDate: opts.publishEndDate?.toISO(),
  299. publishStartDate: opts.publishStartDate?.toISO(),
  300. relations: opts.relations ?? [],
  301. siteId: opts.siteId,
  302. title: opts.title,
  303. toc: '[]',
  304. scripts: JSON.stringify({
  305. jsLoad: scriptJsLoad,
  306. jsUnload: scriptJsUnload,
  307. css: scriptCss
  308. })
  309. }).returning('*')
  310. // -> Save Tags
  311. if (opts.tags && opts.tags.length > 0) {
  312. await WIKI.db.tags.associateTags({ tags: opts.tags, page })
  313. }
  314. // -> Render page to HTML
  315. await WIKI.db.pages.renderPage(page)
  316. return page
  317. // TODO: Handle remaining flow
  318. // -> Rebuild page tree
  319. await WIKI.db.pages.rebuildTree()
  320. // -> Add to Search Index
  321. const pageContents = await WIKI.db.pages.query().findById(page.id).select('render')
  322. page.safeContent = WIKI.db.pages.cleanHTML(pageContents.render)
  323. await WIKI.data.searchEngine.created(page)
  324. // -> Add to Storage
  325. if (!opts.skipStorage) {
  326. await WIKI.db.storage.pageEvent({
  327. event: 'created',
  328. page
  329. })
  330. }
  331. // -> Reconnect Links
  332. await WIKI.db.pages.reconnectLinks({
  333. locale: page.localeCode,
  334. path: page.path,
  335. mode: 'create'
  336. })
  337. // -> Get latest updatedAt
  338. page.updatedAt = await WIKI.db.pages.query().findById(page.id).select('updatedAt').then(r => r.updatedAt)
  339. return page
  340. }
  341. /**
  342. * Update an Existing Page
  343. *
  344. * @param {Object} opts Page Properties
  345. * @returns {Promise} Promise of the Page Model Instance
  346. */
  347. static async updatePage(opts) {
  348. // -> Fetch original page
  349. const ogPage = await WIKI.db.pages.query().findById(opts.id)
  350. if (!ogPage) {
  351. throw new Error('ERR_PAGE_NOT_FOUND')
  352. }
  353. // -> Check for page access
  354. if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
  355. locale: ogPage.localeCode,
  356. path: ogPage.path
  357. })) {
  358. throw new Error('ERR_PAGE_UPDATE_FORBIDDEN')
  359. }
  360. const patch = {}
  361. const historyData = {
  362. action: 'updated',
  363. affectedFields: []
  364. }
  365. // -> Create version snapshot
  366. await WIKI.db.pageHistory.addVersion(ogPage)
  367. // -> Basic fields
  368. if ('title' in opts.patch) {
  369. patch.title = opts.patch.title.trim()
  370. historyData.affectedFields.push('title')
  371. if (patch.title.length < 1) {
  372. throw new Error('ERR_PAGE_TITLE_MISSING')
  373. }
  374. }
  375. if ('description' in opts.patch) {
  376. patch.description = opts.patch.description.trim()
  377. historyData.affectedFields.push('description')
  378. }
  379. if ('icon' in opts.patch) {
  380. patch.icon = opts.patch.icon.trim()
  381. historyData.affectedFields.push('icon')
  382. }
  383. if ('content' in opts.patch) {
  384. patch.content = opts.patch.content
  385. historyData.affectedFields.push('content')
  386. }
  387. // -> Publish State
  388. if (opts.patch.publishState) {
  389. patch.publishState = opts.patch.publishState
  390. historyData.affectedFields.push('publishState')
  391. if (patch.publishState === 'scheduled' && (!opts.patch.publishStartDate || !opts.patch.publishEndDate)) {
  392. throw new Error('ERR_PAGE_MISSING_SCHEDULED_DATES')
  393. }
  394. }
  395. if (opts.patch.publishStartDate) {
  396. patch.publishStartDate = opts.patch.publishStartDate
  397. historyData.affectedFields.push('publishStartDate')
  398. }
  399. if (opts.patch.publishEndDate) {
  400. patch.publishEndDate = opts.patch.publishEndDate
  401. historyData.affectedFields.push('publishEndDate')
  402. }
  403. // -> Page Config
  404. if ('isBrowsable' in opts.patch) {
  405. patch.config = {
  406. ...patch.config ?? ogPage.config ?? {},
  407. isBrowsable: opts.patch.isBrowsable
  408. }
  409. historyData.affectedFields.push('isBrowsable')
  410. }
  411. if ('allowComments' in opts.patch) {
  412. patch.config = {
  413. ...patch.config ?? ogPage.config ?? {},
  414. allowComments: opts.patch.allowComments
  415. }
  416. historyData.affectedFields.push('allowComments')
  417. }
  418. if ('allowContributions' in opts.patch) {
  419. patch.config = {
  420. ...patch.config ?? ogPage.config ?? {},
  421. allowContributions: opts.patch.allowContributions
  422. }
  423. historyData.affectedFields.push('allowContributions')
  424. }
  425. if ('allowRatings' in opts.patch) {
  426. patch.config = {
  427. ...patch.config ?? ogPage.config ?? {},
  428. allowRatings: opts.patch.allowRatings
  429. }
  430. historyData.affectedFields.push('allowRatings')
  431. }
  432. if ('showSidebar' in opts.patch) {
  433. patch.config = {
  434. ...patch.config ?? ogPage.config ?? {},
  435. showSidebar: opts.patch.showSidebar
  436. }
  437. historyData.affectedFields.push('showSidebar')
  438. }
  439. if ('showTags' in opts.patch) {
  440. patch.config = {
  441. ...patch.config ?? ogPage.config ?? {},
  442. showTags: opts.patch.showTags
  443. }
  444. historyData.affectedFields.push('showTags')
  445. }
  446. if ('showToc' in opts.patch) {
  447. patch.config = {
  448. ...patch.config ?? ogPage.config ?? {},
  449. showToc: opts.patch.showToc
  450. }
  451. historyData.affectedFields.push('showToc')
  452. }
  453. if ('tocDepth' in opts.patch) {
  454. patch.config = {
  455. ...patch.config ?? ogPage.config ?? {},
  456. tocDepth: opts.patch.tocDepth
  457. }
  458. historyData.affectedFields.push('tocDepth')
  459. if (patch.config.tocDepth?.min < 1 || patch.config.tocDepth?.min > 6) {
  460. throw new Error('ERR_PAGE_INVALID_TOC_DEPTH')
  461. }
  462. if (patch.config.tocDepth?.max < 1 || patch.config.tocDepth?.max > 6) {
  463. throw new Error('ERR_PAGE_INVALID_TOC_DEPTH')
  464. }
  465. }
  466. // -> Relations
  467. if ('relations' in opts.patch) {
  468. patch.relations = opts.patch.relations.map(r => {
  469. if (r.label.length < 1) {
  470. throw new Error('ERR_PAGE_RELATION_LABEL_MISSING')
  471. } else if (r.label.length > 255) {
  472. throw new Error('ERR_PAGE_RELATION_LABEL_TOOLONG')
  473. } else if (r.icon.length > 255) {
  474. throw new Error('ERR_PAGE_RELATION_ICON_INVALID')
  475. } else if (r.target.length > 1024) {
  476. throw new Error('ERR_PAGE_RELATION_TARGET_INVALID')
  477. }
  478. return r
  479. })
  480. historyData.affectedFields.push('relations')
  481. }
  482. // -> Format CSS Scripts
  483. if (opts.patch.scriptCss) {
  484. if (WIKI.auth.checkAccess(opts.user, ['write:styles'], {
  485. locale: ogPage.localeCode,
  486. path: ogPage.path
  487. })) {
  488. patch.scripts = {
  489. ...patch.scripts ?? ogPage.scripts ?? {},
  490. css: !_.isEmpty(opts.patch.scriptCss) ? new CleanCSS({ inline: false }).minify(opts.patch.scriptCss).styles : ''
  491. }
  492. historyData.affectedFields.push('scripts.css')
  493. }
  494. }
  495. // -> Format JS Scripts
  496. if (opts.patch.scriptJsLoad) {
  497. if (WIKI.auth.checkAccess(opts.user, ['write:scripts'], {
  498. locale: ogPage.localeCode,
  499. path: ogPage.path
  500. })) {
  501. patch.scripts = {
  502. ...patch.scripts ?? ogPage.scripts ?? {},
  503. jsLoad: opts.patch.scriptJsLoad ?? ''
  504. }
  505. historyData.affectedFields.push('scripts.jsLoad')
  506. }
  507. }
  508. if (opts.patch.scriptJsUnload) {
  509. if (WIKI.auth.checkAccess(opts.user, ['write:scripts'], {
  510. locale: ogPage.localeCode,
  511. path: ogPage.path
  512. })) {
  513. patch.scripts = {
  514. ...patch.scripts ?? ogPage.scripts ?? {},
  515. jsUnload: opts.patch.scriptJsUnload ?? ''
  516. }
  517. historyData.affectedFields.push('scripts.jsUnload')
  518. }
  519. }
  520. // -> Tags
  521. if ('tags' in opts.patch) {
  522. historyData.affectedFields.push('tags')
  523. }
  524. // -> Update page
  525. await WIKI.db.pages.query().patch({
  526. ...patch,
  527. authorId: opts.user.id,
  528. historyData
  529. }).where('id', ogPage.id)
  530. let page = await WIKI.db.pages.getPageFromDb(ogPage.id)
  531. // -> Save Tags
  532. if (opts.patch.tags) {
  533. await WIKI.db.tags.associateTags({ tags: opts.patch.tags, page })
  534. }
  535. // -> Render page to HTML
  536. if (opts.patch.content) {
  537. await WIKI.db.pages.renderPage(page)
  538. }
  539. WIKI.events.outbound.emit('deletePageFromCache', page.hash)
  540. // // -> Update Search Index
  541. // const pageContents = await WIKI.db.pages.query().findById(page.id).select('render')
  542. // page.safeContent = WIKI.db.pages.cleanHTML(pageContents.render)
  543. // await WIKI.data.searchEngine.updated(page)
  544. // -> Update on Storage
  545. // if (!opts.skipStorage) {
  546. // await WIKI.db.storage.pageEvent({
  547. // event: 'updated',
  548. // page
  549. // })
  550. // }
  551. // -> Perform move?
  552. if ((opts.locale && opts.locale !== page.localeCode) || (opts.path && opts.path !== page.path)) {
  553. // -> Check target path access
  554. if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
  555. locale: opts.locale,
  556. path: opts.path
  557. })) {
  558. throw new WIKI.Error.PageMoveForbidden()
  559. }
  560. await WIKI.db.pages.movePage({
  561. id: page.id,
  562. destinationLocale: opts.locale,
  563. destinationPath: opts.path,
  564. user: opts.user
  565. })
  566. }
  567. // -> Get latest updatedAt
  568. page.updatedAt = await WIKI.db.pages.query().findById(page.id).select('updatedAt').then(r => r.updatedAt)
  569. return page
  570. }
  571. /**
  572. * Convert an Existing Page
  573. *
  574. * @param {Object} opts Page Properties
  575. * @returns {Promise} Promise of the Page Model Instance
  576. */
  577. static async convertPage(opts) {
  578. // -> Fetch original page
  579. const ogPage = await WIKI.db.pages.query().findById(opts.id)
  580. if (!ogPage) {
  581. throw new Error('Invalid Page Id')
  582. }
  583. if (ogPage.editor === opts.editor) {
  584. throw new Error('Page is already using this editor. Nothing to convert.')
  585. }
  586. // -> Check for page access
  587. if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
  588. locale: ogPage.localeCode,
  589. path: ogPage.path
  590. })) {
  591. throw new WIKI.Error.PageUpdateForbidden()
  592. }
  593. // -> Check content type
  594. const sourceContentType = ogPage.contentType
  595. const targetContentType = _.get(_.find(WIKI.data.editors, ['key', opts.editor]), `contentType`, 'text')
  596. const shouldConvert = sourceContentType !== targetContentType
  597. let convertedContent = null
  598. // -> Convert content
  599. if (shouldConvert) {
  600. // -> Markdown => HTML
  601. if (sourceContentType === 'markdown' && targetContentType === 'html') {
  602. if (!ogPage.render) {
  603. throw new Error('Aborted conversion because rendered page content is empty!')
  604. }
  605. convertedContent = ogPage.render
  606. const $ = cheerio.load(convertedContent, {
  607. decodeEntities: true
  608. })
  609. if ($.root().children().length > 0) {
  610. // Remove header anchors
  611. $('.toc-anchor').remove()
  612. // Attempt to convert tabsets
  613. $('tabset').each((tabI, tabElm) => {
  614. const tabHeaders = []
  615. // -> Extract templates
  616. $(tabElm).children('template').each((tmplI, tmplElm) => {
  617. if ($(tmplElm).attr('v-slot:tabs') === '') {
  618. $(tabElm).before('<ul class="tabset-headers">' + $(tmplElm).html() + '</ul>')
  619. } else {
  620. $(tabElm).after('<div class="markdown-tabset">' + $(tmplElm).html() + '</div>')
  621. }
  622. })
  623. // -> Parse tab headers
  624. $(tabElm).prev('.tabset-headers').children((i, elm) => {
  625. tabHeaders.push($(elm).html())
  626. })
  627. $(tabElm).prev('.tabset-headers').remove()
  628. // -> Inject tab headers
  629. $(tabElm).next('.markdown-tabset').children((i, elm) => {
  630. if (tabHeaders.length > i) {
  631. $(elm).prepend(`<h2>${tabHeaders[i]}</h2>`)
  632. }
  633. })
  634. $(tabElm).next('.markdown-tabset').prepend('<h1>Tabset</h1>')
  635. $(tabElm).remove()
  636. })
  637. convertedContent = $.html('body').replace('<body>', '').replace('</body>', '').replace(/&#x([0-9a-f]{1,6});/ig, (entity, code) => {
  638. code = parseInt(code, 16)
  639. // Don't unescape ASCII characters, assuming they're encoded for a good reason
  640. if (code < 0x80) return entity
  641. return String.fromCodePoint(code)
  642. })
  643. }
  644. // -> HTML => Markdown
  645. } else if (sourceContentType === 'html' && targetContentType === 'markdown') {
  646. const td = new TurndownService({
  647. bulletListMarker: '-',
  648. codeBlockStyle: 'fenced',
  649. emDelimiter: '*',
  650. fence: '```',
  651. headingStyle: 'atx',
  652. hr: '---',
  653. linkStyle: 'inlined',
  654. preformattedCode: true,
  655. strongDelimiter: '**'
  656. })
  657. td.use(turndownPluginGfm)
  658. td.keep(['kbd'])
  659. td.addRule('subscript', {
  660. filter: ['sub'],
  661. replacement: c => `~${c}~`
  662. })
  663. td.addRule('superscript', {
  664. filter: ['sup'],
  665. replacement: c => `^${c}^`
  666. })
  667. td.addRule('underline', {
  668. filter: ['u'],
  669. replacement: c => `_${c}_`
  670. })
  671. td.addRule('taskList', {
  672. filter: (n, o) => {
  673. return n.nodeName === 'INPUT' && n.getAttribute('type') === 'checkbox'
  674. },
  675. replacement: (c, n) => {
  676. return n.getAttribute('checked') ? '[x] ' : '[ ] '
  677. }
  678. })
  679. td.addRule('removeTocAnchors', {
  680. filter: (n, o) => {
  681. return n.nodeName === 'A' && n.classList.contains('toc-anchor')
  682. },
  683. replacement: c => ''
  684. })
  685. convertedContent = td.turndown(ogPage.content)
  686. // -> Unsupported
  687. } else {
  688. throw new Error('Unsupported source / destination content types combination.')
  689. }
  690. }
  691. // -> Create version snapshot
  692. if (shouldConvert) {
  693. await WIKI.db.pageHistory.addVersion({
  694. ...ogPage,
  695. action: 'updated',
  696. versionDate: ogPage.updatedAt
  697. })
  698. }
  699. // -> Update page
  700. await WIKI.db.pages.query().patch({
  701. contentType: targetContentType,
  702. editor: opts.editor,
  703. ...(convertedContent ? { content: convertedContent } : {})
  704. }).where('id', ogPage.id)
  705. const page = await WIKI.db.pages.getPageFromDb(ogPage.id)
  706. await WIKI.db.pages.deletePageFromCache(page.hash)
  707. WIKI.events.outbound.emit('deletePageFromCache', page.hash)
  708. // -> Update on Storage
  709. await WIKI.db.storage.pageEvent({
  710. event: 'updated',
  711. page
  712. })
  713. }
  714. /**
  715. * Move a Page
  716. *
  717. * @param {Object} opts Page Properties
  718. * @returns {Promise} Promise with no value
  719. */
  720. static async movePage(opts) {
  721. let page
  722. if (_.has(opts, 'id')) {
  723. page = await WIKI.db.pages.query().findById(opts.id)
  724. } else {
  725. page = await WIKI.db.pages.query().findOne({
  726. path: opts.path,
  727. localeCode: opts.locale
  728. })
  729. }
  730. if (!page) {
  731. throw new WIKI.Error.PageNotFound()
  732. }
  733. // -> Validate path
  734. if (opts.destinationPath.includes('.') || opts.destinationPath.includes(' ') || opts.destinationPath.includes('\\') || opts.destinationPath.includes('//')) {
  735. throw new WIKI.Error.PageIllegalPath()
  736. }
  737. // -> Remove trailing slash
  738. if (opts.destinationPath.endsWith('/')) {
  739. opts.destinationPath = opts.destinationPath.slice(0, -1)
  740. }
  741. // -> Remove starting slash
  742. if (opts.destinationPath.startsWith('/')) {
  743. opts.destinationPath = opts.destinationPath.slice(1)
  744. }
  745. // -> Check for source page access
  746. if (!WIKI.auth.checkAccess(opts.user, ['manage:pages'], {
  747. locale: page.localeCode,
  748. path: page.path
  749. })) {
  750. throw new WIKI.Error.PageMoveForbidden()
  751. }
  752. // -> Check for destination page access
  753. if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
  754. locale: opts.destinationLocale,
  755. path: opts.destinationPath
  756. })) {
  757. throw new WIKI.Error.PageMoveForbidden()
  758. }
  759. // -> Check for existing page at destination path
  760. const destPage = await WIKI.db.pages.query().findOne({
  761. path: opts.destinationPath,
  762. localeCode: opts.destinationLocale
  763. })
  764. if (destPage) {
  765. throw new WIKI.Error.PagePathCollision()
  766. }
  767. // -> Create version snapshot
  768. await WIKI.db.pageHistory.addVersion({
  769. ...page,
  770. action: 'moved',
  771. versionDate: page.updatedAt
  772. })
  773. const destinationHash = pageHelper.generateHash({ path: opts.destinationPath, locale: opts.destinationLocale })
  774. // -> Move page
  775. const destinationTitle = (page.title === page.path ? opts.destinationPath : page.title)
  776. await WIKI.db.pages.query().patch({
  777. path: opts.destinationPath,
  778. localeCode: opts.destinationLocale,
  779. title: destinationTitle,
  780. hash: destinationHash
  781. }).findById(page.id)
  782. await WIKI.db.pages.deletePageFromCache(page.hash)
  783. WIKI.events.outbound.emit('deletePageFromCache', page.hash)
  784. // -> Rebuild page tree
  785. await WIKI.db.pages.rebuildTree()
  786. // -> Rename in Search Index
  787. const pageContents = await WIKI.db.pages.query().findById(page.id).select('render')
  788. page.safeContent = WIKI.db.pages.cleanHTML(pageContents.render)
  789. await WIKI.data.searchEngine.renamed({
  790. ...page,
  791. destinationPath: opts.destinationPath,
  792. destinationLocaleCode: opts.destinationLocale,
  793. destinationHash
  794. })
  795. // -> Rename in Storage
  796. if (!opts.skipStorage) {
  797. await WIKI.db.storage.pageEvent({
  798. event: 'renamed',
  799. page: {
  800. ...page,
  801. destinationPath: opts.destinationPath,
  802. destinationLocaleCode: opts.destinationLocale,
  803. destinationHash,
  804. moveAuthorId: opts.user.id,
  805. moveAuthorName: opts.user.name,
  806. moveAuthorEmail: opts.user.email
  807. }
  808. })
  809. }
  810. // -> Reconnect Links : Changing old links to the new path
  811. await WIKI.db.pages.reconnectLinks({
  812. sourceLocale: page.localeCode,
  813. sourcePath: page.path,
  814. locale: opts.destinationLocale,
  815. path: opts.destinationPath,
  816. mode: 'move'
  817. })
  818. // -> Reconnect Links : Validate invalid links to the new path
  819. await WIKI.db.pages.reconnectLinks({
  820. locale: opts.destinationLocale,
  821. path: opts.destinationPath,
  822. mode: 'create'
  823. })
  824. }
  825. /**
  826. * Delete an Existing Page
  827. *
  828. * @param {Object} opts Page Properties
  829. * @returns {Promise} Promise with no value
  830. */
  831. static async deletePage(opts) {
  832. const page = await WIKI.db.pages.getPageFromDb(_.has(opts, 'id') ? opts.id : opts)
  833. if (!page) {
  834. throw new WIKI.Error.PageNotFound()
  835. }
  836. // -> Check for page access
  837. if (!WIKI.auth.checkAccess(opts.user, ['delete:pages'], {
  838. locale: page.locale,
  839. path: page.path
  840. })) {
  841. throw new WIKI.Error.PageDeleteForbidden()
  842. }
  843. // -> Create version snapshot
  844. await WIKI.db.pageHistory.addVersion({
  845. ...page,
  846. action: 'deleted',
  847. versionDate: page.updatedAt
  848. })
  849. // -> Delete page
  850. await WIKI.db.pages.query().delete().where('id', page.id)
  851. await WIKI.db.pages.deletePageFromCache(page.hash)
  852. WIKI.events.outbound.emit('deletePageFromCache', page.hash)
  853. // -> Rebuild page tree
  854. await WIKI.db.pages.rebuildTree()
  855. // -> Delete from Search Index
  856. await WIKI.data.searchEngine.deleted(page)
  857. // -> Delete from Storage
  858. if (!opts.skipStorage) {
  859. await WIKI.db.storage.pageEvent({
  860. event: 'deleted',
  861. page
  862. })
  863. }
  864. // -> Reconnect Links
  865. await WIKI.db.pages.reconnectLinks({
  866. locale: page.localeCode,
  867. path: page.path,
  868. mode: 'delete'
  869. })
  870. }
  871. /**
  872. * Reconnect links to new/move/deleted page
  873. *
  874. * @param {Object} opts - Page parameters
  875. * @param {string} opts.path - Page Path
  876. * @param {string} opts.locale - Page Locale Code
  877. * @param {string} [opts.sourcePath] - Previous Page Path (move only)
  878. * @param {string} [opts.sourceLocale] - Previous Page Locale Code (move only)
  879. * @param {string} opts.mode - Page Update mode (create, move, delete)
  880. * @returns {Promise} Promise with no value
  881. */
  882. static async reconnectLinks (opts) {
  883. const pageHref = `/${opts.locale}/${opts.path}`
  884. let replaceArgs = {
  885. from: '',
  886. to: ''
  887. }
  888. switch (opts.mode) {
  889. case 'create':
  890. replaceArgs.from = `<a href="${pageHref}" class="is-internal-link is-invalid-page">`
  891. replaceArgs.to = `<a href="${pageHref}" class="is-internal-link is-valid-page">`
  892. break
  893. case 'move':
  894. const prevPageHref = `/${opts.sourceLocale}/${opts.sourcePath}`
  895. replaceArgs.from = `<a href="${prevPageHref}" class="is-internal-link is-valid-page">`
  896. replaceArgs.to = `<a href="${pageHref}" class="is-internal-link is-valid-page">`
  897. break
  898. case 'delete':
  899. replaceArgs.from = `<a href="${pageHref}" class="is-internal-link is-valid-page">`
  900. replaceArgs.to = `<a href="${pageHref}" class="is-internal-link is-invalid-page">`
  901. break
  902. default:
  903. return false
  904. }
  905. let affectedHashes = []
  906. // -> Perform replace and return affected page hashes (POSTGRES only)
  907. if (WIKI.config.db.type === 'postgres') {
  908. const qryHashes = await WIKI.db.pages.query()
  909. .returning('hash')
  910. .patch({
  911. render: WIKI.db.knex.raw('REPLACE(??, ?, ?)', ['render', replaceArgs.from, replaceArgs.to])
  912. })
  913. .whereIn('pages.id', function () {
  914. this.select('pageLinks.pageId').from('pageLinks').where({
  915. 'pageLinks.path': opts.path,
  916. 'pageLinks.localeCode': opts.locale
  917. })
  918. })
  919. affectedHashes = qryHashes.map(h => h.hash)
  920. } else {
  921. // -> Perform replace, then query affected page hashes (MYSQL, MARIADB, MSSQL, SQLITE only)
  922. await WIKI.db.pages.query()
  923. .patch({
  924. render: WIKI.db.knex.raw('REPLACE(??, ?, ?)', ['render', replaceArgs.from, replaceArgs.to])
  925. })
  926. .whereIn('pages.id', function () {
  927. this.select('pageLinks.pageId').from('pageLinks').where({
  928. 'pageLinks.path': opts.path,
  929. 'pageLinks.localeCode': opts.locale
  930. })
  931. })
  932. const qryHashes = await WIKI.db.pages.query()
  933. .column('hash')
  934. .whereIn('pages.id', function () {
  935. this.select('pageLinks.pageId').from('pageLinks').where({
  936. 'pageLinks.path': opts.path,
  937. 'pageLinks.localeCode': opts.locale
  938. })
  939. })
  940. affectedHashes = qryHashes.map(h => h.hash)
  941. }
  942. for (const hash of affectedHashes) {
  943. await WIKI.db.pages.deletePageFromCache(hash)
  944. WIKI.events.outbound.emit('deletePageFromCache', hash)
  945. }
  946. }
  947. /**
  948. * Rebuild page tree for new/updated/deleted page
  949. *
  950. * @returns {Promise} Promise with no value
  951. */
  952. static async rebuildTree() {
  953. const rebuildJob = await WIKI.scheduler.registerJob({
  954. name: 'rebuild-tree',
  955. immediate: true,
  956. worker: true
  957. })
  958. return rebuildJob.finished
  959. }
  960. /**
  961. * Trigger the rendering of a page
  962. *
  963. * @param {Object} page Page Model Instance
  964. * @returns {Promise} Promise with no value
  965. */
  966. static async renderPage(page) {
  967. const renderJob = await WIKI.scheduler.addJob({
  968. task: 'render-page',
  969. payload: {
  970. id: page.id
  971. },
  972. maxRetries: 0,
  973. promise: true
  974. })
  975. return renderJob.promise
  976. }
  977. /**
  978. * Fetch an Existing Page from Cache if possible, from DB otherwise and save render to Cache
  979. *
  980. * @param {Object} opts Page Properties
  981. * @returns {Promise} Promise of the Page Model Instance
  982. */
  983. static async getPage(opts) {
  984. return WIKI.db.pages.getPageFromDb(opts)
  985. // -> Get from cache first
  986. let page = await WIKI.db.pages.getPageFromCache(opts)
  987. if (!page) {
  988. // -> Get from DB
  989. page = await WIKI.db.pages.getPageFromDb(opts)
  990. if (page) {
  991. if (page.render) {
  992. // -> Save render to cache
  993. await WIKI.db.pages.savePageToCache(page)
  994. } else {
  995. // -> No render? Possible duplicate issue
  996. /* TODO: Detect duplicate and delete */
  997. throw new Error('Error while fetching page. No rendered version of this page exists. Try to edit the page and save it again.')
  998. }
  999. }
  1000. }
  1001. return page
  1002. }
  1003. /**
  1004. * Fetch an Existing Page from the Database
  1005. *
  1006. * @param {Object} opts Page Properties
  1007. * @returns {Promise} Promise of the Page Model Instance
  1008. */
  1009. static async getPageFromDb(opts) {
  1010. const queryModeID = typeof opts === 'string'
  1011. try {
  1012. return WIKI.db.pages.query()
  1013. .column([
  1014. 'pages.*',
  1015. {
  1016. authorName: 'author.name',
  1017. authorEmail: 'author.email',
  1018. creatorName: 'creator.name',
  1019. creatorEmail: 'creator.email'
  1020. }
  1021. ])
  1022. .joinRelated('author')
  1023. .joinRelated('creator')
  1024. .withGraphJoined('tags')
  1025. .modifyGraph('tags', builder => {
  1026. builder.select('tag')
  1027. })
  1028. .where(queryModeID ? {
  1029. 'pages.id': opts
  1030. } : {
  1031. 'pages.siteId': opts.siteId,
  1032. 'pages.path': opts.path,
  1033. 'pages.localeCode': opts.locale
  1034. })
  1035. // .andWhere(builder => {
  1036. // if (queryModeID) return
  1037. // builder.where({
  1038. // 'pages.isPublished': true
  1039. // }).orWhere({
  1040. // 'pages.isPublished': false,
  1041. // 'pages.authorId': opts.userId
  1042. // })
  1043. // })
  1044. .first()
  1045. } catch (err) {
  1046. WIKI.logger.warn(err)
  1047. throw err
  1048. }
  1049. }
  1050. /**
  1051. * Save a Page Model Instance to Cache
  1052. *
  1053. * @param {Object} page Page Model Instance
  1054. * @returns {Promise} Promise with no value
  1055. */
  1056. static async savePageToCache(page) {
  1057. const cachePath = path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${page.hash}.bin`)
  1058. await fs.outputFile(cachePath, WIKI.db.pages.cacheSchema.encode({
  1059. id: page.id,
  1060. authorId: page.authorId,
  1061. authorName: page.authorName,
  1062. createdAt: page.createdAt.toISOString(),
  1063. creatorId: page.creatorId,
  1064. creatorName: page.creatorName,
  1065. description: page.description,
  1066. editor: page.editor,
  1067. extra: {
  1068. css: _.get(page, 'extra.css', ''),
  1069. js: _.get(page, 'extra.js', '')
  1070. },
  1071. publishState: page.publishState ?? '',
  1072. publishEndDate: page.publishEndDate ?? '',
  1073. publishStartDate: page.publishStartDate ?? '',
  1074. render: page.render,
  1075. siteId: page.siteId,
  1076. tags: page.tags.map(t => _.pick(t, ['tag'])),
  1077. title: page.title,
  1078. toc: _.isString(page.toc) ? page.toc : JSON.stringify(page.toc),
  1079. updatedAt: page.updatedAt.toISOString()
  1080. }))
  1081. }
  1082. /**
  1083. * Fetch an Existing Page from Cache
  1084. *
  1085. * @param {Object} opts Page Properties
  1086. * @returns {Promise} Promise of the Page Model Instance
  1087. */
  1088. static async getPageFromCache(opts) {
  1089. const pageHash = pageHelper.generateHash({ path: opts.path, locale: opts.locale })
  1090. const cachePath = path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${pageHash}.bin`)
  1091. try {
  1092. const pageBuffer = await fs.readFile(cachePath)
  1093. let page = WIKI.db.pages.cacheSchema.decode(pageBuffer)
  1094. return {
  1095. ...page,
  1096. path: opts.path,
  1097. localeCode: opts.locale
  1098. }
  1099. } catch (err) {
  1100. if (err.code === 'ENOENT') {
  1101. return false
  1102. }
  1103. WIKI.logger.error(err)
  1104. throw err
  1105. }
  1106. }
  1107. /**
  1108. * Delete an Existing Page from Cache
  1109. *
  1110. * @param {String} page Page Unique Hash
  1111. * @returns {Promise} Promise with no value
  1112. */
  1113. static async deletePageFromCache(hash) {
  1114. return fs.remove(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${hash}.bin`))
  1115. }
  1116. /**
  1117. * Flush the contents of the Cache
  1118. */
  1119. static async flushCache() {
  1120. return fs.emptyDir(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache`))
  1121. }
  1122. /**
  1123. * Migrate all pages from a source locale to the target locale
  1124. *
  1125. * @param {Object} opts Migration properties
  1126. * @param {string} opts.sourceLocale Source Locale Code
  1127. * @param {string} opts.targetLocale Target Locale Code
  1128. * @returns {Promise} Promise with no value
  1129. */
  1130. static async migrateToLocale({ sourceLocale, targetLocale }) {
  1131. return WIKI.db.pages.query()
  1132. .patch({
  1133. localeCode: targetLocale
  1134. })
  1135. .where({
  1136. localeCode: sourceLocale
  1137. })
  1138. .whereNotExists(function() {
  1139. this.select('id').from('pages AS pagesm').where('pagesm.localeCode', targetLocale).andWhereRaw('pagesm.path = pages.path')
  1140. })
  1141. }
  1142. /**
  1143. * Clean raw HTML from content for use in search engines
  1144. *
  1145. * @param {string} rawHTML Raw HTML
  1146. * @returns {string} Cleaned Content Text
  1147. */
  1148. static cleanHTML(rawHTML = '') {
  1149. let data = striptags(rawHTML || '', [], ' ')
  1150. .replace(emojiRegex(), '')
  1151. // .replace(htmlEntitiesRegex, '')
  1152. return he.decode(data)
  1153. .replace(punctuationRegex, ' ')
  1154. .replace(/(\r\n|\n|\r)/gm, ' ')
  1155. .replace(/\s\s+/g, ' ')
  1156. .split(' ').filter(w => w.length > 1).join(' ').toLowerCase()
  1157. }
  1158. /**
  1159. * Subscribe to HA propagation events
  1160. */
  1161. static subscribeToEvents() {
  1162. WIKI.events.inbound.on('deletePageFromCache', hash => {
  1163. WIKI.db.pages.deletePageFromCache(hash)
  1164. })
  1165. WIKI.events.inbound.on('flushCache', () => {
  1166. WIKI.db.pages.flushCache()
  1167. })
  1168. }
  1169. }