post_asset.js 869 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. const { Schema } = require('warehouse');
  3. const { join } = require('path');
  4. module.exports = ctx => {
  5. const PostAsset = new Schema({
  6. _id: {type: String, required: true},
  7. slug: {type: String, required: true},
  8. modified: {type: Boolean, default: true},
  9. post: {type: Schema.Types.CUID, ref: 'Post'},
  10. renderable: {type: Boolean, default: true}
  11. });
  12. PostAsset.virtual('path').get(function() {
  13. const Post = ctx.model('Post');
  14. const post = Post.findById(this.post);
  15. if (!post) return;
  16. // PostAsset.path is file path relative to `public_dir`
  17. // no need to urlescape, #1562
  18. // strip /\.html?$/ extensions on permalink, #2134
  19. return join(post.path.replace(/\.html?$/, ''), this.slug);
  20. });
  21. PostAsset.virtual('source').get(function() {
  22. return join(ctx.base_dir, this._id);
  23. });
  24. return PostAsset;
  25. };