Ghost/core/server/models/snippet.js
Kevin Ansfield c2793eedd3 Fixed no-restricted-require warning for core/shared/url-utils.js
no issue

Our server-defined `mobiledoc` object was required by `UrlUtils.cardTransformers` property to help set up a shortcut for url transform functions but that was breaking the independence of `UrlUtils` by crossing the shared/server boundary.

- `cardTransformers` is only needed for the `mobiledocToTransformReady` utility function that will only be used by the server
- removed `UrlUtils.cardTransformers` (and associated require) from our `UrlUtils` instance and updated the few areas the server uses `mobiledocToTransformReady()` to pass in the mobiledoc card objects directly as an option
2021-05-25 21:13:57 +01:00

35 lines
911 B
JavaScript

const ghostBookshelf = require('./base');
const urlUtils = require('../../shared/url-utils');
const mobiledocLib = require('../lib/mobiledoc');
const Snippet = ghostBookshelf.Model.extend({
tableName: 'snippets',
formatOnWrite(attrs) {
if (attrs.mobiledoc) {
attrs.mobiledoc = urlUtils.mobiledocToTransformReady(attrs.mobiledoc, {cardTransformers: mobiledocLib.cards});
}
return attrs;
},
parse() {
const attrs = ghostBookshelf.Model.prototype.parse.apply(this, arguments);
if (attrs.mobiledoc) {
attrs.mobiledoc = urlUtils.transformReadyToAbsolute(attrs.mobiledoc);
}
return attrs;
}
});
const Snippets = ghostBookshelf.Collection.extend({
model: Snippet
});
module.exports = {
Snippet: ghostBookshelf.model('Snippet', Snippet),
Snippets: ghostBookshelf.collection('Snippets', Snippets)
};