Comments (3)
Would love this. I've just jumped through so many hoops to make a code block appear full bleed with some truly horrific css that could have been avoided with one extra wrapping div.
from eleventy-plugin-syntaxhighlight.
Hi, if I understand correctly, you're trying to wrap the highlighted code block in a div
like this?
<div>
<pre>
<code>
<!-- highlighted code -->
</code>
</pre>
</div>
I agree, it seems like a feature that Prism should support natively. However, as a workaround in Eleventy, if you're using markdown-it as your markdown parser, you can override the renderer rules for fenced code blocks like this:
const defaultRender = markdownLibrary.renderer.rules.fence;
markdownLibrary.renderer.rules.fence = function (
tokens,
idx,
options,
env,
slf
) {
let html = defaultRender(tokens, idx, options, env, slf);
html = `<div class="wrapper">${html}</div>`;
return html;
};
This will wrap the highlighted code from the syntax highlighter plugin in a div
with the class wrapper
.
![image](https://private-user-images.githubusercontent.com/3301828/339625554-1b7b9066-a428-40fb-954b-a654a12ffdef.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTg5NzgzNzYsIm5iZiI6MTcxODk3ODA3NiwicGF0aCI6Ii8zMzAxODI4LzMzOTYyNTU1NC0xYjdiOTA2Ni1hNDI4LTQwZmItOTU0Yi1hNjU0YTEyZmZkZWYucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDYyMSUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDA2MjFUMTM1NDM2WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9NDU3NDhiMmVkN2Y0NGYyMzNkNjI2ZWY1Y2E4MGI4MjlkOGFiNzY2YTE3YTQxNzc3YzFkZmU5NzYzYzJiNDdjZCZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.h-Ws30ZxcrAjMgo8AI-JEIeyVKpxTYjC7K0p23QzNww)
This approach should work with Liquid, Nunjucks, or any other template engines that interpret markdown to generate HTML.
And note this will only affect fenced code blocks (i.e., code blocks that are enclosed in triple backticks ```). If you want to apply the same wrapping to indented code blocks (i.e., code blocks that are indented by four spaces or a tab), you would need to override markdownLibrary.renderer.rules.code_block in the same way.
Hope that helps anyone looking to do this.
from eleventy-plugin-syntaxhighlight.
In the meantime, I came up with a workaround. It’s clunky, but it does the job.
- My posts with code blocks all use the same template, so it now has this in place of the usual
{{ content | safe }}
:
{# START, divs around Prism `pre``code` stuff #}
{% set Content = content %}
{% set withoutDivStart = '<pre class="language-' %}
{% set withDivStart = '<div class="highlight"><pre class="language-' %}
{% set withoutDivEnd = '</code></pre>' %}
{% set withDivEnd = '</code></pre></div>' %}
{% if withoutDivStart in content %}
{% set Content = content | replace (withoutDivStart, withDivStart) %}
{% set Content = Content | replace (withoutDivEnd, withDivEnd) %}
{% endif %}
{# END, divs around Prism `pre``code` stuff #}
{{ Content | safe }}
(Note the capitalization or lack thereof for “Content”/“content” above.)
- I added the following CSS to the relevant place:
div.highlight {
position: relative;
}
- I adjusted the code for the “copy code” button (borrowed from others’ efforts, as noted in https://www.brycewray.com/posts/2022/05/gems-in-rough-18/#code-for-copying-code) so that it would put the button before the
pre
, not after:
const pre = codeBlock.parentNode;
pre.parentNode.insertBefore(button, pre);
Now, if you have to scroll horizontally through a long-width code block, the “copy code” button stays affixed to the right end of the div
as it should rather than scrolling with the code.
Mind you, this doesn’t cancel my feature request 😄 — but it does work in the interim, so there ya go.
from eleventy-plugin-syntaxhighlight.
Related Issues (20)
- Added and removed lines get no special styling HOT 1
- Providing a default language for fenced code blocks HOT 8
- Add prismjs alias for `nunjucks` mapped to `jinja2` HOT 2
- Unsupported Languages cause Build Failures HOT 3
- Inline code support HOT 1
- Compatibility with markdown-it-attrs HOT 1
- Upgrade to use Torchlight? HOT 6
- Using the diff function, output is not escaped. HOT 3
- Cannot mix `diff` and `diff-lang` in one app HOT 1
- Syntax highlighting can fail if another plugin uses Prism too
- diff-javascript code block generates invalid HTML HOT 1
- Remove character wrap feature.
- Change default `lineSeparator` option to `\n` instead of `<br>` HOT 1
- possibility of adding plugins
- possibility of adding plugins
- possibility of adding Prismjs plugins to a config
- possibility of adding Prismjs plugins to a config
- [Discussion] Allowing users to override Prism instance
- If I'm using Ghost as a data source and it provides article content in HTML code, how can I do code highlighting?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eleventy-plugin-syntaxhighlight.