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
|
||||
haste_document.prototype.load = function(key, callback, lang) {
|
||||
var _this = this;
|
||||
var selectedLines = this.app.selectedLines;
|
||||
$.ajax(_this.app.baseUrl + 'documents/' + key, {
|
||||
type: 'get',
|
||||
dataType: 'json',
|
||||
|
@ -27,15 +28,30 @@ haste_document.prototype.load = function(key, callback, lang) {
|
|||
_this.key = key;
|
||||
_this.data = res.data;
|
||||
try {
|
||||
var high;
|
||||
if (lang === 'txt') {
|
||||
high = { value: _this.htmlEscape(res.data) };
|
||||
var high = { value: "" };
|
||||
var lines = res.data.split("\n");
|
||||
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;
|
||||
}
|
||||
else if (lang) {
|
||||
high = hljs.highlight(lang, res.data);
|
||||
|
||||
var currentLine = i + 1;
|
||||
if (
|
||||
currentLine >= selectedLines.startLine &&
|
||||
currentLine <= selectedLines.endLine
|
||||
) {
|
||||
highlighted =
|
||||
'<span style="background-color: yellow;">' + highlighted + "</span>";
|
||||
}
|
||||
else {
|
||||
high = hljs.highlightAuto(res.data);
|
||||
|
||||
highlighted = "<span id='line-" + i + "'>" + highlighted + "</span>";
|
||||
high.value += highlighted + "\n";
|
||||
}
|
||||
} catch (err) {
|
||||
// failed highlight, fall back on auto
|
||||
|
@ -104,6 +120,7 @@ var haste = function(appName, options) {
|
|||
$('#box2 .twitter').hide();
|
||||
};
|
||||
this.baseUrl = options.baseUrl || '/';
|
||||
this.selectedLines = options.selectedLines;
|
||||
};
|
||||
|
||||
// Set the page title - include the appName
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
var handlePop = function(evt) {
|
||||
var path = evt.target.location.href;
|
||||
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
|
||||
// to make chrome behave like others:
|
||||
|
@ -29,11 +29,41 @@
|
|||
try { handlePop(evt); } catch(err) { /* not loaded yet */ }
|
||||
};
|
||||
}, 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
|
||||
$(function() {
|
||||
var baseUrl = window.location.href.split('/');
|
||||
var baseUrl = window.location.href.split('#')[0].split('/');
|
||||
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 });
|
||||
});
|
||||
</script>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue