Jekyll Code snippet copy-to-clipboard button - jekyll

The Problem
I am building a Jekyll site with the minima theme to publish some tutorial online. The tutorial pages contain many code snippets, for example:
```javascript
/* Global scope: this code is executed once */
const redis = require('redis');
const host = <HOSTNAME>;
const port = <PORT>;
const password = <PASSWORD>;
...
```
I would like to add a "copy to clipboard" button to each code snippet (example), but not sure what's the right way to do it in Jekyll.
What have I tried
Using clipboardjs.com. It requires a unique ID for each snippet, and I'm not sure how to implement this in Jekyll/Markdown.
STFW
My question
How can I add a "Copy to Clipboard" button for code snippets in Jekyll?

Manually generate id's for each block of code with kramdown's Block Inline Attribute Lists, adding {: #code-example-1} after it.
In your example:
```javascript
/* Global scope: this code is executed once */
const redis = require('redis');
const host = <HOSTNAME>;
const port = <PORT>;
const password = <PASSWORD>;
...
```
{: #code-example-1}
That will generate:
<div id="code-example-1" class="language-javascript highlighter-rouge">
....
</div>
using jquery
Code blocks use thecode html element, if we detect it, then we load the js, traverse all code elements adding a custom id, and a button to copy their content. Finally initialize the Clipboard buttons.
{% if page.content contains "code" %}
<script>
<!-- clipboard.js code -->
</script>
{% endif %}
// get all <code> elements
var allCodeBlocksElements = $( "code" );
allCodeBlocksElements.each(function(i) {
// add different id for each code block
// target
var currentId = "codeblock" + (i + 1);
$(this).attr('id', currentId);
//trigger
var clipButton = '<button class="btn" data-clipboard-target="#' + currentId + '"><img src="https://clipboardjs.com/assets/images/clippy.svg" width="13" alt="Copy to clipboard"></button>';
$(this).after(clipButton);
});
new Clipboard('.btn');
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/clipboard#1/dist/clipboard.min.js"></script>
<code>print("Club Nacional de Football")</code>
<br>
<code>print("is a sports institution")</code>
<br>
<code>print("from Uruguay")</code>

Let's use includes.
tuto.md
---
front matter things here
---
{%- capture code -%}
/* Some js code */
const redis = require('redis');
const host = <HOSTNAME>;
{%- endcapture -%}
{% include code_snippet.md code=code language='javascript' %}
{%- capture code -%}
# Some ruby code
t = Time.now
t.succ
{%- endcapture -%}
{% include code_snippet.md code=code language='ruby' %}
_includes/code_snippet.md
{% assign code = include.code %}
{% assign language = include.language %}
``` {{ language }}
{{ code }}
```
{% assign nanosecond = "now" | date: "%N" %}
<textarea id="code{{ nanosecond }}" style="display:none;">{{ code | xml_escape }}</textarea>
<button id="copybutton{{ nanosecond }}" data-clipboard-target="#code{{ nanosecond }}">
Copy to clipboard
</button>
<script>
var copybutton = document.getElementById('copybutton{{ nanosecond }}');
var clipboard{{ nanosecond }} = new Clipboard(copybutton);
clipboard{{ nanosecond }}.on('success', function(e) {
console.log(e);
});
clipboard{{ nanosecond }}.on('error', function(e) {
console.log(e);
});
</script>

Related

Smooth Scolling in CSS and HTML

Hello I am working on my website with multiple pages and I want to do smooth scrolling on a certain page but I don't want to use the html tag because it will only be for this specific page and not the whole website here is my code.
{% if section.settings.display_product_detail_description == false and section.settings.display_product_reviews == false and section.settings.display_shipping_returns == false %}
{% assign tab5_active = true %}
{% endif %}
<div class="scroll-to-table">
<li class = "tab-title">
<a href="#product_attributes_table" class="tab-links {% if tab5_active %} active {% endif %}">
Specs
</a>
</li>
</div>
This is the code for the HTML
div.scroll-to-table{
scroll-behavior: smooth;}
And here is the code for the CSS
At the moment all that the page is doing is a jump and not a smooth scroll. I've tried using ID instead of Class, .scroll-to-table instead of div.scroll-to-table, and changing the element in which I call the CSS from but no luck
We can add scroll-behaviour: smooth to particular page by using javascript IIFE (Immediately Invoked Function Expression). This will add the smooth behaviour to the only page that runs this script.
<script>
(function () {
document.documentElement.style.scrollBehavior = smooth;
})();
</script>

For loop and Popup with liquid and jekyll

I want to create a list of modal popups with a for-loop, each of them displaying different text.
The site is created with Jekyll with the Liquid templating engine.
In particular, I want to create the list of my scientific publications, for each of them with 2 icons: one for the bibtex entry and one for the abstract. This information is stored in a yaml file.
I m following this simple tutorial for modal popups.
The popups work, but the text is the same for all the entries. How is possible to generate independent modal popups?
This is the html
{% for papers in papers %}
{% for content in paper.papers %}
<a title="{{content.name}}"><i class='{{content.icon}}' data-modal-target="#modal"></i></a>
<div class="modal" id="modal">
<div class="modal-header">
<div class="title">{{content.name}}</div>
<button data-close-button class="close-button">×</button>
</div>
<!-- text to display -->
<div class="modal-body">{{content.text}}</div>
</div>
{% endfor %}
{% endfor %}
and this is the Javascript code:
const openModalIcons = document.querySelectorAll('[data-modal-target]')
const closeModalButtons = document.querySelectorAll('[data-close-button]')
const overlay = document.getElementById('overlay')
openModalIcons.forEach(icon => {
icon.addEventListener('click', () => {
const modal = document.querySelector(icon.dataset.modalTarget)
openModal(modal)
})
})
function openModal(modal) {
if (modal == null) return
modal.classList.add('active')
overlay.classList.add('active')
}
closeModalButtons.forEach(button => {
button.addEventListener('click', () => {
const modal = button.closest('.modal')
closeModal(modal)
})
})
function closeModal(modal) {
if (modal == null) return
modal.classList.remove('active')
overlay.classList.remove('active')
}
overlay.addEventListener('click', () => {
const modals = document.querySelectorAll('.modal.active')
modals.forEach(modal => {
closeModal(modal)
})
})
The problem is that all your modals have id="modal", so the selector #modal is probably always selecting the first one. The id attribute should be unique in the entire document.
Something like this should work:
{% for papers in papers %}
{% for content in paper.papers %}
<a title="{{content.name}}"><i class='{{content.icon}}' data-modal-target="#paperModal{{forloop.parentloop.counter}}_{{forloop.counter}}"></i></a>
<div class="modal" id="paperModal{{forloop.parentloop.counter}}_{{forloop.counter}}">
<div class="modal-header">
<div class="title">{{content.name}}</div>
<button data-close-button class="close-button">×</button>
</div>
<!-- text to display -->
<div class="modal-body">{{content.text}}</div>
</div>
{% endfor %}
{% endfor %}
Instead of the for loop counter, you could also use the paper name as the ID (as long as it's unique), e.g. id="paperModal_{{content.name | slugify}}".
Edit: Edited the snippet to use forloop.counter and account for the nested for loop.

Render Template FLASK

I have a problem with reload page, when I click the button wchich is used to filtrate data witch using ajax script. In condition IF not working render template .
Below sample code
Python
def viewchallenges():
categories = Categories.query.all()
if request.method=='POST':
category_id=Categories.query.filter_by(category=request.form['name']).first()
check=Challenge.query.filter_by(categorie_id=category_id.id).order_by(Challenge.timestamp.desc()).all()
if not check:
flash(_('not found'))
else:
page = request.args.get('page', 1, type=int)
posts = Challenge.query.filter_by(categorie_id=category_id.id).order_by(Challenge.timestamp.desc()).paginate(
page, current_app.config['POSTS_PER_PAGE'], False)
next_url = url_for('main.viewchallenges', page=posts.next_num) \
if posts.has_next else None
prev_url = url_for('main.viewchallenges', page=posts.prev_num) \
if posts.has_prev else None
return render_template('viewChallanges.html', title=_('View Challenge'), posts=posts.items, next_url=next_url, prev_url=prev_url,categories=categories)
page = request.args.get('page', 1, type=int)
posts =Challenge.query.order_by(Challenge.timestamp.desc()).paginate(
page,current_app.config['POSTS_PER_PAGE'], False)
next_url = url_for('main.viewchallenges', page=posts.next_num) \
if posts.has_next else None
prev_url = url_for('main.viewchallenges', page=posts.prev_num) \
if posts.has_prev else None
return render_template('viewChallanges.html', title=_('View Challenge'), posts=posts.items, next_url=next_url, prev_url=prev_url,categories=categories)
HTML
{% for item_category in categories %}
<button class="btn btn-secondary fby_category">{{item_category.category}}</button>
{% endfor %}
AJAX
$('.fby_category').click(function(e) {
var url = "{{ url_for('main.viewchallenges') }}";
e.preventDefault();
$.ajax({
type: "POST",
url: url,
data: {
'name': $(this).text()
},
});
});
You want to render a new template. It means that the user is going to get a new webpage. Then you do not need Javascript here. You can just create multiple forms
{% for item_category in categories %}
<form action="{{ url_for('main.viewchallenges') }}" method="post">
<input type="submit" name="{{item_category.category}}<" value="
{{item_category.category}}" />
{% endfor %}
</form>
Easier, isn't ?

How to make this particular rendered content in HTML as downloadable text file in Django?

<div class="container" >
{% if sr %}
{% for k in sr %}
<p>Name: {{k.name}}</p>
<p>Email: {{k.email}}</p>
<p>Contact Number: {{k.contact_number}}</p>
{% endfor %}
{% endif %}
</div>
THis is my html code and it is rendering perfectly when executed but i tried writing a function to download the rendered content as text file. And it is downloading the html code as text file not the actual rendered content. Please help me fix this. Thanks in advance.
You can begin by declaring the following Javascript function:
function download(filename, text) {
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
Then you will need a function to call your download function. This function should have your text and filename:
function beginDownload(){
// Generate download of the text file with some content
var text = document.getElementById('text-content').innerHTML;
var filename = "text.txt";
download(filename, text);
}
Add the following id to your div:
<div class="container" id='text-content'>
. . .
</div>
Next add a button that will initiate the download:
<button type="button" onclick="beginDownload()">Download</button>
Edit:
Replace the following:
var text = document.getElementById('text-content').innerHTML;
with:
var text = document.getElementById('text-content').innerText;
to get the text only

Include a twig file and pass variables from a separate file?

I have container.twig including component.twig and passing an object called 'mock'.
In container.twig:
{% set mock = {
title : "This is my title"
}
%}
{% include 'component.twig' with mock %}
This is working fine but I want to move the mock data to its own file. This isnt working:
Container.twig
{% include 'component.twig' with 'mock.twig' %}
In mock.twig
{% set mock = {
title : "This is my title"
}
%}
Im using gulp-twig but it works like standard twig in most respects. https://github.com/zimmen/gulp-twig
The problem
Twig context is never stored in the template object, so this will be very difficult to find a clean way to achieve this. For example, the following Twig code:
{% set test = 'Hello, world' %}
Will compile to:
<?php
class __TwigTemplate_20df0122e7c88760565e671dea7b7d68c33516f833acc39288f926e234b08380 extends Twig_Template
{
/* ... */
protected function doDisplay(array $context, array $blocks = array())
{
// line 1
$context["test"] = "Hello, world";
}
/* ... */
}
As you can see, the inherited context is not passed to the doDisplay method by reference, and is never stored in the object itself (like $this->context = $context). This deisgn allow templates to be reusable, and is memory-friendly.
Solution 1 : using global variables
I don't know if you are aware of Global Variables in Twig. You can do a bunch of hacks with them.
The easiest usage is to load all your globals inside your twig environment.
$loader = new Twig_Loader_Filesystem(__DIR__.'/view');
$env = new Twig_Environment($loader);
$env->addGlobal('foo', 'bar');
$env->addGlobal('Hello', 'world!');
Then, you can use {{ foo }} and {{ Hello }} in your whole application.
But there are 2 problems here:
As you're trying to load variables from twig files, I assume you have lots of variables to initialize depending on your feature and don't want to load everything all time.
you are loading variables from PHP scripts and not from Twig, and your question want to import variables from a twig file.
Solution 2 : using a Twig extension
You can also create a storage extension that provide a save function to persist some template's context somewhere, and a restore function to merge this stored context in another one.
proof_of_concept.php
<?php
require __DIR__.'/vendor/autoload.php';
class StorageTwigExtension extends Twig_Extension
{
protected $storage = [];
public function getFunctions() {
return [
new Twig_SimpleFunction('save', [$this, 'save'], ['needs_context' => true]),
new Twig_SimpleFunction('restore', [$this, 'restore'], ['needs_context' => true]),
];
}
public function save($context, $name) {
$this->storage = array_merge($this->storage, $context);
}
public function restore(&$context, $name) {
$context = array_merge($context, $this->storage);
}
public function getName() {
return 'storage';
}
}
/* usage example */
$loader = new Twig_Loader_Filesystem(__DIR__.'/view');
$env = new Twig_Environment($loader);
$env->addExtension(new StorageTwigExtension());
echo $env->render('test.twig'), PHP_EOL;
twig/variables.twig
{% set foo = 'bar' %}
{% set Hello = 'world!' %}
{{ save('test') }}
twig/test.twig
{% include 'variables.twig' %}
{{ restore('test') }}
{{ foo }}
Note: if you only want to import variables without actually rendering what's inside twig/variables.twig, you can also use:
{% set tmp = include('variables.twig') %}
{{ restore('test') }}
{{ foo }}
Final note
I'm not used to the JavaScript twig port, but it looks like you can still extend it, that's your go :)
Because of Twig's scoping rules (which I assume are replicated by the gulp version), you cannot pass variables up from a child template without creating a helper function. The closest thing you can do is to use inheritance to replicate this.
As such, your mock.twig file would become
{% set mock = {
title : "This is my title"
}
%}
{% block content %}{% endblock %}
Your container.twig would then become
{% extends 'mock.twig' %}
{% block content %}
{% include 'component.twig' with mock %}
{% endblock %}
This achieves your goals of separating the mock content from the templates for the most part and, using dynamic extends, you can do something like
{% extends usemock == 'true'
? 'contentdumper.twig'
: 'mock.twig' %}
with a contentdumper.twig file that is just a stub like so
{% block content %}{% endblock %}
and then set the usemock variable to determine if you will be using the mock data or not.
Hopefully this helps, even though it doesn't really solve the exact problem you are having.