mirror of
https://github.com/seejohnrun/haste-server
synced 2025-08-21 03:23:11 -07:00
initial commit, added highlight lines if url has params
This commit is contained in:
parent
87a6a88449
commit
7da109f1a6
2 changed files with 61 additions and 14 deletions
|
@ -19,6 +19,7 @@ haste_document.prototype.htmlEscape = function(s) {
|
||||||
// Get this document from the server and lock it here
|
// Get this document from the server and lock it here
|
||||||
haste_document.prototype.load = function(key, callback, lang) {
|
haste_document.prototype.load = function(key, callback, lang) {
|
||||||
var _this = this;
|
var _this = this;
|
||||||
|
var selectedLines = this.app.selectedLines;
|
||||||
$.ajax(_this.app.baseUrl + 'documents/' + key, {
|
$.ajax(_this.app.baseUrl + 'documents/' + key, {
|
||||||
type: 'get',
|
type: 'get',
|
||||||
dataType: 'json',
|
dataType: 'json',
|
||||||
|
@ -27,20 +28,35 @@ haste_document.prototype.load = function(key, callback, lang) {
|
||||||
_this.key = key;
|
_this.key = key;
|
||||||
_this.data = res.data;
|
_this.data = res.data;
|
||||||
try {
|
try {
|
||||||
var high;
|
var high = { value: "" };
|
||||||
if (lang === 'txt') {
|
var lines = res.data.split("\n");
|
||||||
high = { value: _this.htmlEscape(res.data) };
|
for (var i = 0; i < lines.length; i++) {
|
||||||
|
if (lang === "txt") {
|
||||||
|
highlighted = _this.htmlEscape(res.data);
|
||||||
|
} else if (lang) {
|
||||||
|
highlighted = hljs.highlight(lang, lines[i]).value;
|
||||||
|
} else {
|
||||||
|
var highlightedData = hljs.highlightAuto(lines[i]);
|
||||||
|
high.language = highlightedData.language;
|
||||||
|
highlighted = highlightedData.value;
|
||||||
|
}
|
||||||
|
|
||||||
|
var currentLine = i + 1;
|
||||||
|
if (
|
||||||
|
currentLine >= selectedLines.startLine &&
|
||||||
|
currentLine <= selectedLines.endLine
|
||||||
|
) {
|
||||||
|
highlighted =
|
||||||
|
'<span style="background-color: yellow;">' + highlighted + "</span>";
|
||||||
|
}
|
||||||
|
|
||||||
|
highlighted = "<span id='line-" + i + "'>" + highlighted + "</span>";
|
||||||
|
high.value += highlighted + "\n";
|
||||||
}
|
}
|
||||||
else if (lang) {
|
} catch (err) {
|
||||||
high = hljs.highlight(lang, res.data);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
high = hljs.highlightAuto(res.data);
|
|
||||||
}
|
|
||||||
} catch(err) {
|
|
||||||
// failed highlight, fall back on auto
|
// failed highlight, fall back on auto
|
||||||
high = hljs.highlightAuto(res.data);
|
high = hljs.highlightAuto(res.data);
|
||||||
}
|
}
|
||||||
callback({
|
callback({
|
||||||
value: high.value,
|
value: high.value,
|
||||||
key: key,
|
key: key,
|
||||||
|
@ -104,6 +120,7 @@ var haste = function(appName, options) {
|
||||||
$('#box2 .twitter').hide();
|
$('#box2 .twitter').hide();
|
||||||
};
|
};
|
||||||
this.baseUrl = options.baseUrl || '/';
|
this.baseUrl = options.baseUrl || '/';
|
||||||
|
this.selectedLines = options.selectedLines;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Set the page title - include the appName
|
// Set the page title - include the appName
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
var handlePop = function(evt) {
|
var handlePop = function(evt) {
|
||||||
var path = evt.target.location.href;
|
var path = evt.target.location.href;
|
||||||
if (path === app.baseUrl) { app.newDocument(true); }
|
if (path === app.baseUrl) { app.newDocument(true); }
|
||||||
else { app.loadDocument(path.split('/').slice(-1)[0]); }
|
else { app.loadDocument(path.split('#')[0].split('/').slice(-1)[0]); }
|
||||||
};
|
};
|
||||||
// Set up the pop state to handle loads, skipping the first load
|
// Set up the pop state to handle loads, skipping the first load
|
||||||
// to make chrome behave like others:
|
// to make chrome behave like others:
|
||||||
|
@ -29,11 +29,41 @@
|
||||||
try { handlePop(evt); } catch(err) { /* not loaded yet */ }
|
try { handlePop(evt); } catch(err) { /* not loaded yet */ }
|
||||||
};
|
};
|
||||||
}, 1000);
|
}, 1000);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function to parse the URL fragment and extract line numbers
|
||||||
|
* @returns {Object} Object with parsed startLine, endLine
|
||||||
|
*/
|
||||||
|
function getSelectedLinesFromURL() {
|
||||||
|
const urlHash = window.location.hash.substring(1); // Remove the '#' from the hash
|
||||||
|
if (!urlHash) return {}; // Return an empty object if there's no hash
|
||||||
|
|
||||||
|
// Parse the hash into start and end parts
|
||||||
|
const range = urlHash.split("L");
|
||||||
|
let start, end;
|
||||||
|
|
||||||
|
if (range.length > 2) {
|
||||||
|
start = parseInt(range[1], 10);
|
||||||
|
end = parseInt(range[2], 10);
|
||||||
|
} else {
|
||||||
|
start = parseInt(range[1], 10);
|
||||||
|
end = start;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
startLine: start,
|
||||||
|
endLine: end,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Construct app and load initial path
|
// Construct app and load initial path
|
||||||
$(function() {
|
$(function() {
|
||||||
var baseUrl = window.location.href.split('/');
|
var baseUrl = window.location.href.split('#')[0].split('/');
|
||||||
baseUrl = baseUrl.slice(0, baseUrl.length - 1).join('/') + '/';
|
baseUrl = baseUrl.slice(0, baseUrl.length - 1).join('/') + '/';
|
||||||
app = new haste('hastebin', { twitter: true, baseUrl: baseUrl });
|
console.log(baseUrl);
|
||||||
|
const selectedLines = getSelectedLinesFromURL();
|
||||||
|
console.log(selectedLines);
|
||||||
|
app = new haste('hastebin', { twitter: true, baseUrl: baseUrl, selectedLines: selectedLines });
|
||||||
handlePop({ target: window });
|
handlePop({ target: window });
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue