Skip to content

Commit

Permalink
Update line number alignement
Browse files Browse the repository at this point in the history
  • Loading branch information
szabi committed Mar 8, 2024
1 parent 1f893cc commit 8c33508
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ breaking changes to the options supported by the plugin will be shipped as a bre

## Unreleased changes

- (minor) Align code-block line numbers to the right.
<!--
All changes being submitted through PRs should be added to this section.
Please add a new list item to the top of this section with a summary of the change.
Expand Down
1 change: 1 addition & 0 deletions dev/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const md = require('markdown-it')({
fence_classes: {
allowedClasses: [
'prefixed', 'line_numbers', 'command', 'super_user', 'custom_prefix',
'line-number-width-2', 'line-number-width-3', 'line-number-width-4', 'line-number-width-5',
...[ 'local', 'second', 'third', 'fourth', 'fifth' ].map(env => `environment-${env}`),
],
},
Expand Down
7 changes: 7 additions & 0 deletions modifiers/fence_classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ module.exports = (md, options) => {

// Extract the class attribute
const classes = tag.slice(classPos.start + 7, classPos.end - 1).split(' ');

// Calculate and append line number width class if needed.
const lineNumberDigitCount = (content.match(/<li/g) || []).length.toString().length;
if (lineNumberDigitCount > 1) {
classes.push(`line-number-width-${lineNumberDigitCount}`);
}

const permitted = classes.filter(cls => optsObj.allowedClasses.includes(cls));

// Generate the new tag
Expand Down
45 changes: 45 additions & 0 deletions styles/_code_prefix.scss
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,56 @@ pre {

&::before {
border-right: 1px solid rgba($white, 0.5);
direction: rtl;
padding-right: 5px;
}
}
}
}
&.line-number-width-2 {
code {
ol {
li {
&::before {
width: 3ch;
}
}
}
}
}
&.line-number-width-3 {
code {
ol {
li {
&::before {
width: 4ch;
}
}
}
}
}
&.line-number-width-4 {
code {
ol {
li {
&::before {
width: 5ch;
}
}
}
}
}
&.line-number-width-5 {
code {
ol {
li {
&::before {
width: 6ch;
}
}
}
}
}
}
}
}

0 comments on commit 8c33508

Please sign in to comment.