mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-26 12:21:36 +03:00
a6f5eb71be
closes https://github.com/TryGhost/Team/issues/467 - switches to storing "transform-ready" URLs in the database - transform-ready URLs contain a `__GHOST_URL__` placeholder that corresponds to the configured url that gives a few benefits - much faster and less memory intensive output transformations through not needing to parse html or markdown - the transform can be achieved using a straightforward regex find+replace - ability to change to/from or rename subdirectory without any manual updates to the database - modified existing 4.0 url-transformation migration rather than adding another one and repeating the transformation on posts rows
39 lines
1.1 KiB
JavaScript
39 lines
1.1 KiB
JavaScript
const ghostBookshelf = require('./base');
|
|
const urlUtils = require('../../shared/url-utils');
|
|
|
|
const Snippet = ghostBookshelf.Model.extend({
|
|
tableName: 'snippets',
|
|
|
|
onSaving: function onSaving() {
|
|
const urlTransformMap = {
|
|
mobiledoc: 'mobiledocToTransformReady'
|
|
};
|
|
|
|
Object.entries(urlTransformMap).forEach(([attr, transform]) => {
|
|
let method = transform;
|
|
let methodOptions = {};
|
|
|
|
if (typeof transform === 'object') {
|
|
method = transform.method;
|
|
methodOptions = transform.options || {};
|
|
}
|
|
|
|
if (this.hasChanged(attr) && this.get(attr)) {
|
|
const transformedValue = urlUtils[method](this.get(attr), methodOptions);
|
|
this.set(attr, transformedValue);
|
|
}
|
|
});
|
|
|
|
ghostBookshelf.Model.prototype.onSaving.apply(this, arguments);
|
|
}
|
|
});
|
|
|
|
const Snippets = ghostBookshelf.Collection.extend({
|
|
model: Snippet
|
|
});
|
|
|
|
module.exports = {
|
|
Snippet: ghostBookshelf.model('Snippet', Snippet),
|
|
Snippets: ghostBookshelf.collection('Snippets', Snippets)
|
|
};
|