add dynamic seo settings

This commit is contained in:
2025-10-13 18:40:05 +02:00
parent 0d826e8fe2
commit 78af7b2906
4 changed files with 127 additions and 54 deletions

View File

@@ -53,20 +53,40 @@ Haste.prototype.newDocument = function(hideHistory) {
// load an existing document
Haste.prototype.loadDocument = function(key) {
const _this = this;
_this.doc = new HasteDocument();
_this.doc.load(key, function(ret) {
if (ret) {
_this.$code.html(ret.value);
_this.setTitle(ret.key);
window.history.pushState(null, `${_this.appName}-${ret.key}`, `/${ret.key}`);
_this.fullKey();
_this.$textarea.val('').hide();
_this.$box.show();
} else {
_this.newDocument();
}
});
// Check if data is pre-rendered by the server
const $preloaded = $('#preloaded-data');
if ($preloaded.length > 0 && $preloaded.text().trim().length > 0) {
this.doc = new HasteDocument();
this.doc.locked = true;
this.doc.key = key;
this.doc.data = $preloaded.text();
const high = hljs.highlightAuto(this.doc.data).value;
const ret = {
value: high.replace(/.+/g, "<span class=\"line\">$&</span>").replace(/^\s*[\r\n]/gm, "<span class=\"line\"></span>\n"),
key: key,
};
this.$code.html(ret.value);
this.setTitle(ret.key);
this.fullKey();
this.$textarea.val('').hide();
this.$box.show();
} else {
// Data is not pre-rendered, fetch from API
const _this = this;
_this.doc = new HasteDocument();
_this.doc.load(key, function(ret) {
if (ret) {
_this.$code.html(ret.value);
_this.setTitle(ret.key);
window.history.pushState(null, `${_this.appName}-${ret.key}`, `/${ret.key}`);
_this.fullKey();
_this.$textarea.val('').hide();
_this.$box.show();
} else {
_this.newDocument();
}
});
}
};
// duplicate the current document
@@ -83,7 +103,10 @@ Haste.prototype.lockDocument = function() {
const _this = this;
this.doc.save(this.$textarea.val(), function(err, ret) {
if (!err && ret) {
_this.$code.html(ret.value.trim().replace(/.+/g, "<span class=\"line\">$&</span>").replace(/^\s*[\r\n]/gm, "<span class=\"line\"></span>\n"));
// Manually highlight and display after saving
const high = hljs.highlightAuto(ret.data).value;
const formatted = high.replace(/.+/g, "<span class=\"line\">$&</span>").replace(/^\s*[\r\n]/gm, "<span class=\"line\"></span>\n");
_this.$code.html(formatted);
_this.setTitle(ret.key);
window.history.pushState(null, `${_this.appName}-${ret.key}`, `/${ret.key}`);
_this.fullKey();
@@ -114,7 +137,7 @@ Haste.prototype.configureButtons = function() {
return evt.ctrlKey && evt.keyCode === 32;
},
action: function() {
_this.newDocument(!_this.doc.key);
window.location.href = '/';
}
},
{
@@ -172,15 +195,6 @@ const HasteDocument = function() {
this.locked = false;
};
// escape HTML-characters
HasteDocument.prototype.htmlEscape = function(s) {
return s
.replace(/&/g, '&amp;')
.replace(/>/g, '&gt;')
.replace(/</g, '&lt;')
.replace(/"/g, '&quot;');
};
// load a document from the server
HasteDocument.prototype.load = function(key, callback) {
const _this = this;
@@ -207,14 +221,18 @@ HasteDocument.prototype.load = function(key, callback) {
HasteDocument.prototype.save = function(data, callback) {
if (this.locked) return false;
this.data = data;
const _this = this;
_this.data = data;
$.ajax('/documents', {
type: 'post',
data: data,
dataType: 'json',
contentType: 'text/plain; charset=utf-8',
success: function(res) {
new Haste().loadDocument(res.key);
_this.locked = true;
_this.key = res.key;
// Pass the saved data back to the callback
callback(null, { key: res.key, data: _this.data });
},
error: function(res) {
try {
@@ -256,11 +274,11 @@ $(function() {
const app = new Haste();
const path = window.location.pathname;
if (path === '/') {
if (path === '/' || path === '') {
app.newDocument(true);
} else {
app.loadDocument(path.substring(1, path.length));
app.loadDocument(path.substring(1));
}
});
hljs.initHighlightingOnLoad();
// No need for hljs.initHighlightingOnLoad() as we do it manually