wiki/server/models/pages.js
/**
   * Create a New Page
   *
   * @param {Object} opts Page Properties
   * @returns {Promise} Promise of the Page Model Instance
   */
Create a New Page
@param {Object} opts Page Properties
@returns {Promise} Promise of the Page Model Instance
Create a New Page @param { Object } opts Page Properties @returns { Promise } Promise of the Page Model Instance
static async createPage(opts) {
    if (opts.path.includes('.') || opts.path.includes(' ') || opts.path.includes('\\') || opts.path.includes('//')) {
      throw new WIKI.Error.PageIllegalPath()
    }
    if (opts.path.endsWith('/')) {
      opts.path = opts.path.slice(0, -1)
    }
    if (opts.path.startsWith('/')) {
      opts.path = opts.path.slice(1)
    }
    if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
      locale: opts.locale,
      path: opts.path
    })) {
      throw new WIKI.Error.PageDeleteForbidden()
    }
    const dupCheck = await WIKI.models.pages.query().select('id').where('localeCode', opts.locale).where('path', opts.path).first()
    if (dupCheck) {
      throw new WIKI.Error.PageDuplicateCreate()
    }
    if (!opts.content || _.trim(opts.content).length < 1) {
      throw new WIKI.Error.PageEmptyContent()
    }
    let scriptCss = ''
    if (WIKI.auth.checkAccess(opts.user, ['write:styles'], {
      locale: opts.locale,
      path: opts.path
    })) {
      if (!_.isEmpty(opts.scriptCss)) {
        scriptCss = new CleanCSS({ inline: false }).minify(opts.scriptCss).styles
      } else {
        scriptCss = ''
      }
    }
    let scriptJs = ''
    if (WIKI.auth.checkAccess(opts.user, ['write:scripts'], {
      locale: opts.locale,
      path: opts.path
    })) {
      scriptJs = opts.scriptJs || ''
    }
    await WIKI.models.pages.query().insert({
      authorId: opts.user.id,
      content: opts.content,
      creatorId: opts.user.id,
      contentType: _.get(_.find(WIKI.data.editors, ['key', opts.editor]), `contentType`, 'text'),
      description: opts.description,
      editorKey: opts.editor,
      hash: pageHelper.generateHash({ path: opts.path, locale: opts.locale, privateNS: opts.isPrivate ? 'TODO' : '' }),
      isPrivate: opts.isPrivate,
      isPublished: opts.isPublished,
      localeCode: opts.locale,
      path: opts.path,
      publishEndDate: opts.publishEndDate || '',
      publishStartDate: opts.publishStartDate || '',
      title: opts.title,
      toc: '[]',
      extra: JSON.stringify({
        js: scriptJs,
        css: scriptCss
      })
    })
    const page = await WIKI.models.pages.getPageFromDb({
      path: opts.path,
      locale: opts.locale,
      userId: opts.user.id,
      isPrivate: opts.isPrivate
    })
    if (opts.tags && opts.tags.length > 0) {
      await WIKI.models.tags.associateTags({ tags: opts.tags, page })
    }
    await WIKI.models.pages.renderPage(page)
    await WIKI.models.pages.rebuildTree()
    const pageContents = await WIKI.models.pages.query().findById(page.id).select('render')
    page.safeContent = WIKI.models.pages.cleanHTML(pageContents.render)
    await WIKI.data.searchEngine.created(page)
    if (!opts.skipStorage) {
      await WIKI.models.storage.pageEvent({
        event: 'created',
        page
      })
    }
    await WIKI.models.pages.reconnectLinks({
      locale: page.localeCode,
      path: page.path,
      mode: 'create'
    })
    page.updatedAt = await WIKI.models.pages.query().findById(page.id).select('updatedAt').then(r => r.updatedAt)
    return page
  }
static async createPage ( opts ) { if ( opts . path . includes ( '.' ) || opts . path . includes ( ' ' ) || opts . path . includes ( '\\' ) || opts . path . includes ( '//' ) ) { throw new WIKI . Error . PageIllegalPath ( ) } if ( opts . path . endsWith ( '/' ) ) { opts . path = opts . path . slice ( 0 , - 1 ) } if ( opts . path . startsWith ( '/' ) ) { opts . path = opts . path . slice ( 1 ) } if ( ! WIKI . auth . checkAccess ( opts . user , [ 'write:pages' ] , { locale : opts . locale , path : opts . path } ) ) { throw new WIKI . Error . PageDeleteForbidden ( ) } const dupCheck = await WIKI . models . pages . query ( ) . select ( 'id' ) . where ( 'localeCode' , opts . locale ) . where ( 'path' , opts . path ) . first ( ) if ( dupCheck ) { throw new WIKI . Error . PageDuplicateCreate ( ) } if ( ! opts . content || _ . trim ( opts . content ) . length < 1 ) { throw new WIKI . Error . PageEmptyContent ( ) } let scriptCss = '' if ( WIKI . auth . checkAccess ( opts . user , [ 'write:styles' ] , { locale : opts . locale , path : opts . path } ) ) { if ( ! _ . isEmpty ( opts . scriptCss ) ) { scriptCss = new CleanCSS ( { inline : false } ) . minify ( opts . scriptCss ) . styles } else { scriptCss = '' } } let scriptJs = '' if ( WIKI . auth . checkAccess ( opts . user , [ 'write:scripts' ] , { locale : opts . locale , path : opts . path } ) ) { scriptJs = opts . scriptJs || '' } await WIKI . models . pages . query ( ) . insert ( { authorId : opts . user . id , content : opts . content , creatorId : opts . user . id , contentType : _ . get ( _ . find ( WIKI . data . editors , [ 'key' , opts . editor ] ) , ` ` , 'text' ) , description : opts . description , editorKey : opts . editor , hash : pageHelper . generateHash ( { path : opts . path , locale : opts . locale , privateNS : opts . isPrivate ? 'TODO' : '' } ) , isPrivate : opts . isPrivate , isPublished : opts . isPublished , localeCode : opts . locale , path : opts . path , publishEndDate : opts . publishEndDate || '' , publishStartDate : opts . publishStartDate || '' , title : opts . title , toc : '[]' , extra : JSON . stringify ( { js : scriptJs , css : scriptCss } ) } ) const page = await WIKI . models . pages . getPageFromDb ( { path : opts . path , locale : opts . locale , userId : opts . user . id , isPrivate : opts . isPrivate } ) if ( opts . tags && opts . tags . length > 0 ) { await WIKI . models . tags . associateTags ( { tags : opts . tags , page } ) } await WIKI . models . pages . renderPage ( page ) await WIKI . models . pages . rebuildTree ( ) const pageContents = await WIKI . models . pages . query ( ) . findById ( page . id ) . select ( 'render' ) page . safeContent = WIKI . models . pages . cleanHTML ( pageContents . render ) await WIKI . data . searchEngine . created ( page ) if ( ! opts . skipStorage ) { await WIKI . models . storage . pageEvent ( { event : 'created' , page } ) } await WIKI . models . pages . reconnectLinks ( { locale : page . localeCode , path : page . path , mode : 'create' } ) page . updatedAt = await WIKI . models . pages . query ( ) . findById ( page . id ) . select ( 'updatedAt' ) . then ( r => r . updatedAt ) return page }
Create a New Page
 @param {Object} opts Page Properties
 @returns {Promise} Promise of the Page Model Instance
Create a New Page @param { Object } opts Page Properties @returns { Promise } Promise of the Page Model Instance
// -> Validate path // -> Remove trailing slash // -> Remove starting slash // -> Check for page access // -> Check for duplicate // -> Check for empty content // -> Format CSS Scripts // -> Format JS Scripts // -> Create page // -> Save Tags // -> Render page to HTML // -> Rebuild page tree // -> Add to Search Index // -> Add to Storage // -> Reconnect Links // -> Get latest updatedAt