From 7da109f1a693176ff2a51d01448306f6518a3df7 Mon Sep 17 00:00:00 2001 From: Colin Date: Mon, 22 Jul 2024 15:33:21 -0700 Subject: [PATCH] initial commit, added highlight lines if url has params --- static/application.js | 39 ++++++++++++++++++++++++++++----------- static/index.html | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 61 insertions(+), 14 deletions(-) diff --git a/static/application.js b/static/application.js index 370bc74..8aef747 100644 --- a/static/application.js +++ b/static/application.js @@ -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,20 +28,35 @@ 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; + } + + var currentLine = i + 1; + if ( + currentLine >= selectedLines.startLine && + currentLine <= selectedLines.endLine + ) { + highlighted = + '' + highlighted + ""; + } + + highlighted = "" + highlighted + ""; + high.value += highlighted + "\n"; } - else if (lang) { - high = hljs.highlight(lang, res.data); - } - else { - high = hljs.highlightAuto(res.data); - } - } catch(err) { + } catch (err) { // failed highlight, fall back on auto high = hljs.highlightAuto(res.data); - } + } callback({ value: high.value, key: key, @@ -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 diff --git a/static/index.html b/static/index.html index 840fb6b..41a87ff 100644 --- a/static/index.html +++ b/static/index.html @@ -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 }); });