Closed simonw closed 3 years ago
I'm going to do this by implementing a /-/ripgrep/view/path/to/file.txt#L150
URL scheme, serving content from the configured "path"
.
Useful pattern for CSS line numbers, inspired by https://www.sylvaindurand.org/using-css-to-add-line-numbering/
<style>
pre{
counter-reset: line;
}
code{
counter-increment: line;
}
code:before{
content: counter(line);
display: inline-block;
width: 2em;
-webkit-user-select: none;
color: #666;
}
</style>
<pre>
<code>Line one</code>
<code>Line one</code>
<code>Line one</code>
<code>Line one</code>
<code>Line one</code>
<code>Line one</code>
<code>Line one</code>
<code>Line one</code>
<code>Line one</code>
<code>Line one</code>
<code>Line one</code>
<code>Line one</code>
<code>Line one</code>
<code>Line one</code>
<code>Line one</code>
<code>Line one</code>
</pre>
That width: 2em
should be a number of em
s equal to the width of the highest line number. Even better, use ch
measure.
... except I need to also include id=
attributes so I can link to lines - so those numbers should be embedded in the template, not generated by a CSS counter.
{% block extra_head %}
<style>
code:before {
content: attr(data-line);
display: inline-block;
width: {{ widest_line_number + 1.5 }}ch;
-webkit-user-select: none;
color: #666;
}
</style>
{% endblock %}
{% block content %}
<pre>{% for i, line in lines -%}
<code id="L{{ i + 1 }}" data-line="{{ i + 1 }}">{{ line if line else " "|safe }}</code>
{% endfor %}</pre>
Need to be sure this is protected against path traversal attacks, so you can only view files that are within the path
directory hierarchy.
That last commit should not have referenced this issue.
You should be able to click the line number in a search result to view the line in context of the rest of the source code.