Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow permalinking to lines #43

Closed
wants to merge 2 commits into from
Closed

Conversation

ammaraskar
Copy link

Allows you to click on lines and grab a hash based permalink to it in your url bar, much like in github.

Demonstration: http://paste.md-5.net/pomiqeyeye.cs#L17

There is one hack in here I'm not real proud of: https://github.com/seejohnrun/haste-server/pull/43/files#L3R21 but I can't really figure out a nice way to work around that properly. Basically when you change the hash, it calls the pop event because the history changes. Feel free to offer any better solutions to the problem if you have any.

@jamietech jamietech mentioned this pull request Apr 13, 2013
@Ribesg
Copy link

Ribesg commented Aug 29, 2013

This breaks JavaDoc styling.
http://paste.md-5.net/rijecejego.java
BUT, it fixes the fact that JD is too much indented (Compare http://hastebin.com/jiniyidade.java and http://paste.md-5.net/witiwawogi.java )

@Ribesg
Copy link

Ribesg commented Aug 29, 2013

@ammaraskar Ok here's a kind-of fix:

// Injects code with line spans in to the code box
haste.prototype.setCode = function(code) {
  var lines = new Array();

  // Will contain the parent classes
  var currentContainerClasses = [];

  $.each(code.split("\n"), function (index, value) {

    // Line numbers aren't 0 based :)
    var index = index + 1;

    // Use a string containing all parent classes, or just line if there's none
    var classString = currentContainerClasses.length == 0 ? 'line' : currentContainerClasses.join(' ');
    lines[index] = "<span class='" + classString + "' rel='L" + index + "'>" + value + "</span>\n";

    // Count opened and closed spans
    var open = value.match(new RegExp('<span','g'));
    var close = value.match(new RegExp('<\\/span>','g'));
    var openCount = open ? open.length : 0;
    var closeCount = close ? close.length : 0;
    if (openCount > closeCount) {
        // New parent class !
        var firstClassOfValue = value.split(new RegExp('<\\/span>','g'))[0].match(new RegExp('<span class="([a-zA-Z]+)">'))[1];
        currentContainerClasses.push(firstClassOfValue);
    } else if (openCount < closeCount) {
        // Closed a span !
        currentContainerClasses.pop();
    }
  });
  this.$code.html(lines.join(""));
}

It maybe lacks some checks, but it's working.
Good Javadoc colors, clickable and linkable line numbers and no wrong indent. Ultimate combo.

Thanks to SpaceEmotion and kiwhen for helping me with the strange things that regex are.

@FichteFoll
Copy link

Feature request: Can you implement section highlighting like with github? I mean when you shift-click a line and highlight a whole section.

@potomak
Copy link

potomak commented Sep 10, 2013

👍

@johntdyer
Copy link

Any update?

@seejohnrun
Copy link
Contributor

Thanks for the contribution!
I should be able to check this out today - will have something back to you soon (and sorry for the delay)

@ammaraskar
Copy link
Author

Ohey this thing, so the issue mentioned above where it breaks the highlighter on occasion is caused by the fact that I decided to use s for links as seen here: https://github.com/seejohnrun/haste-server/pull/43/files#diff-48f187de8cdff79f6b6ec3a932963c25R235

Sometimes this causes the highlighter's own span tags to be closed off prematurely. I'll probably be able to update this later in the week according to any input from you and that request above to highlight sections

@seejohnrun
Copy link
Contributor

@ammaraskar couldn't we use a different element type than just a flat span?
Other than this looks good!
Thanks!

@@ -201,6 +227,16 @@ haste.prototype.removeLineNumbers = function() {
$('#linenos').html('&gt;');
};

// Injects code with line spans in to the code box
haste.prototype.setCode = function(code) {
var lines = new Array();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change to []

@gt-c
Copy link

gt-c commented Aug 5, 2018

Can I expect this to come soon?

@seejohnrun
Copy link
Contributor

@gt-c Just given the age of the PR we'll need some cleanup here / shouldn't be too hard to get it up to date but it'll be a bit before I have time personally

@gt-c
Copy link

gt-c commented Aug 12, 2018

ok :D thanks!

Copy link
Contributor

@filipechagas filipechagas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ammaraskar
Thank you for contributing!

Can you please verify @seejohnrun comments and also one of my own?
Also, please update the branch with latest master changes.


.line.highlight {
background-color: #004A5E;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please fix EOL?

@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale label Mar 17, 2022
@github-actions
Copy link

This PR was closed because it has been inactive for 14 days since being marked as stale.

@github-actions github-actions bot closed this Mar 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants