Skip to content

Commit

Permalink
Add support for line numbers in Markdown code fences
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyasmith committed Oct 9, 2024
1 parent 559919e commit 83e9ea2
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion packages/kg-markdown-html-renderer/lib/markdown-html-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,35 @@ const namedHeaders = function ({ghostVersion} = {}) {
};
};

const fenceLineNumbers = function (md) {
const proxy = (tokens, idx, options, env, self) => self.renderToken(tokens, idx, options);
const defaultFence = md.renderer.rules.fence || proxy;

// Originally from https://github.com/coreyasmith/markdown-it-fence-line-numbers
md.renderer.rules.fence = function (tokens, idx, options, env, self) {
const token = tokens[idx];
const info = token.info;
const attribute = 'line-numbers';

if (!info) {
return defaultFence(tokens, idx, options, env, self);
}

// line-numbers must come after the first word in the info string to be rendered.
// Example: ```ruby line-numbers
// If line-numbers is specified as the first word, fallback to the default behavior
// (i.e., treat line-numbers as the language).
const langAttrs = info.split(/(\s+)/g).slice(2).join('');
const attributeRegex = new RegExp(`\\b${attribute}\\b`);
if (!langAttrs || !attributeRegex.test(langAttrs)) {
return defaultFence(tokens, idx, options, env, self);
}

token.attrJoin('class', attribute);
return defaultFence(tokens, idx, options, env, self);
};
};

const selectRenderer = function (options) {
const version = semver.coerce(options.ghostVersion || '4.0');

Expand Down Expand Up @@ -77,7 +106,8 @@ const selectRenderer = function (options) {
.use(require('markdown-it-image-lazy-loading'))
.use(namedHeaders(options))
.use(require('markdown-it-sub'))
.use(require('markdown-it-sup'));
.use(require('markdown-it-sup'))
.use(fenceLineNumbers);

markdownIt.linkify.set({
fuzzyLink: false
Expand Down

0 comments on commit 83e9ea2

Please sign in to comment.