Skip to content

Commit

Permalink
basic backlinks implementation, closes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
xplosionmind committed Jun 18, 2022
1 parent 464c72f commit fe3667b
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 80 deletions.
112 changes: 79 additions & 33 deletions content/content.11tydata.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,81 @@
const {titleCase} = require('title-case');
const wikilinkRegExp = /\[\[\s?([^\[\]\|\n\r]+)(\|[^\[\]\|\n\r]+)?\s?\]\]/g // This regex finds all wikilinks in a string
function caselessCompare(a, b) {
return a.toLowerCase() === b.toLowerCase();
}

module.exports = {
permalink: '/{{ page.fileSlug | replace: " ", "-" }}/',
lang: 'en',
layout: 'wrapper',
image: '/tommi.space.wip.png',
// Automatically generating titles, as explained in https://github.com/11ty/eleventy/discussions/2241#discussioncomment-2224265
eleventyComputed: {
title(data) {
// return data.title || deslugify(data.page?.fileSlug);
let hadTitle = false;
const title = data.title || require('lodash').startCase(data.page?.fileSlug);
if (data.title) {
hadTitle = true;
}
// console.log(`${data.page.filePathStem} => ${title}${hadTitle ? " (had title)" : ""}`);
return title;
},
/*date(data) {
let hadDate = false;
const date = data.date || '2020-03-20';
if (data.date) {
hadDate = true;
}
return date;
},*/
updated(data) {
let hadUpdated = false;
const updated = data.updated || data.date;
if (data.updated) {
hadUpdated = true;
}
return updated;
}
}
permalink: '/{{ page.fileSlug | replace: " ", "-" }}/',
lang: 'en',
layout: 'wrapper',
image: '/tommi.space.wip.png',
// Automatically generating titles, as explained in https://github.com/11ty/eleventy/discussions/2241#discussioncomment-2224265
eleventyComputed: {
title(data) {
// return data.title || deslugify(data.page?.fileSlug);
let hadTitle = false;
const title = data.title || require('lodash').startCase(data.page?.fileSlug);
if (data.title) {
hadTitle = true;
}
// console.log(`${data.page.filePathStem} => ${title}${hadTitle ? " (had title)" : ""}`);
return title;
},
/*date(data) {
let hadDate = false;
const date = data.date || '2020-03-20';
if (data.date) {
hadDate = true;
}
return date;
},*/
updated(data) {
let hadUpdated = false;
const updated = data.updated || data.date;
if (data.updated) {
hadUpdated = true;
}
return updated;
},

/************
* Backlinks *
************/
title: data => titleCase(data.title || data.page.fileSlug),
backlinks: (data) => {
const allPages = data.collections.all;
const currentFileSlug = data.page.fileSlug;

let backlinks = [];

// Search for backlinks in every page
for(const otherNote of allPages) {
const noteContent = otherNote.template.frontMatter.content;

// Get all links from otherNote
const outboundLinks = (noteContent.match(wikilinkRegExp) || [])
.map(link => (
// Extract link location
link.slice(2,-2)
.split('|')[0]
.replace(/.(md|html)\s?$/i, '')
.trim()
));

// If the other note links here, return related info
if(outboundLinks.some(link => caselessCompare(link, currentFileSlug))) {

// Construct preview for hovercards
/*let preview = noteContent.slice(0, 240);*/

backlinks.push({
url: otherNote.url,
title: otherNote.data.title//,
//preview
})
}
}
return backlinks;
}
}
};
65 changes: 19 additions & 46 deletions includes/backlinks.html
Original file line number Diff line number Diff line change
@@ -1,48 +1,21 @@
<section class='box' id='backlinks'>
<h2>Backlinks</h2>
<p class='center'>
{% if lang == 'it' %}
Le pagine del sito che contengono un link a questa pagina.<br>
Curioso riguardo cosa voglia dire? Leggi <a href='https://it.wikipedia.org/wiki/Backlink' title='Backlink su Wikipedia'>questo articolo</a>.
{% else %}
All the pages of this website containing a link to the current page.<br>Curious about what this means? Check <a href='https://en.wikipedia.org/wiki/Backlink' target='_blank' title='Backlink on Wikipedia'>this article</a>.
{% endif %}
</p>


{% comment %}#fix not working, probably because of collections not passing through<ul>
{% capture page_url %}{{ raw_url | downcase | remove: '/' }}{% endcapture %}
{% capture done %}{{ page_url }}{% endcapture %}
{% for entry in all_pages %}
{% capture entry_url %}{% entry.url | downcase | remove: '/' %}{% endcapture %}
{% if entry.content contains page_url %}
{% if done != entry_url %}
{% assign done = entry_url %}
<li>
<a href='{{ entry_url }}' title='{{ entry.data.title }}'>{{ entry.data.title }}</a>
{% if entry.data.description %}
- {{ entry.data.description }}
{% else %}
- {{ entry.content | strip_html | truncatewords: 30 }}
{% endif %}
</li>
{% endif %}
{% if backlinks.length > 0 %}
<section class='box' id='backlinks'>
<h2>Backlinks</h2>
<p class='center'>
{% if lang == 'it' %}
Le pagine del sito che contengono un link a questa pagina.<br>
Curioso riguardo cosa voglia dire? Leggi <a href='https://it.wikipedia.org/wiki/Backlink' title='Backlink su Wikipedia'>questo articolo</a>.
{% else %}
All the pages of this website containing a link to the current page.<br>Curious about what this means? Check <a href='https://en.wikipedia.org/wiki/Backlink' title='Backlink on Wikipedia'>this article</a>.
{% endif %}
{% for redirect in redirect_from %}
{% if entry.content contains redirect %}
{% if done != entry_url %}
{% assign done = entry_url %}
<li>
<a href='{{ entry_url }}'>{{ entry.title }}</a>
{% if entry.description %}
- {{ entry.description }}
{% else %}
- {{ entry.excerpt | strip_html | truncatewords: 30 }}…
{% endif %}
</li>
{% endif %}
{% endif %}
</p>

<ul class='two'>
{% for link in backlinks %}
<li class='backlink'>
<a href='{{ link.url }}' title='{{ link.title }}'>{{ link.title }}</a>
</li>
{% endfor %}
{% endfor %}
</ul>{% endcomment %}
</section>
</ul>
</section>
{% endif %}
2 changes: 1 addition & 1 deletion layouts/jam.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ <h3>{% if lang == 'it' %}Indice{% else %}Table of contents{% endif %}</h3>
<div class='one column'>
{% assign encoded_absolute_url = page.url | prepend: site.url | url_encode %}
{% render 'share.html', lang: lang, title: title, encoded_absolute_url: encoded_absolute_url %}
{% comment %}{% render 'backlinks.html', raw_url: page.url, all_pages: collections.all, lang: lang %}{% endcomment %}
{% render 'backlinks.html', lang: lang, backlinks: backlinks %}
{% render 'comments.html', lang: lang %}
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions layouts/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ <h3>{% if lang == 'it' %}Indice{% else %}Table of contents{% endif %}</h3>
<div class='half column'>
{% assign encoded_absolute_url = page.url | prepend: site.url | url_encode %}
{% render 'share.html', lang: lang, title: title, encoded_absolute_url: encoded_absolute_url %}
{% render 'backlinks.html', lang: lang, backlinks: backlinks %}
</div>
<div class='half column'>
{% render 'comments.html', lang: lang %}
Expand Down
1 change: 1 addition & 0 deletions layouts/sconnesso.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<div class='one column'>
{% assign encoded_absolute_url = page.url | prepend: site.url | url_encode %}
{% render 'share.html', lang: lang, title: title, encoded_absolute_url: encoded_absolute_url %}
{% render 'backlinks.html', lang: lang, backlinks: backlinks %}
{% render 'comments.html', lang: lang %}
{% render 'listen-on.html', lang: lang %}
</div>
Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@
"text-table": "^0.2.0",
"tfunk": "^4.0.0",
"time-require": "^0.1.2",
"title-case": "^3.0.3",
"to-array": "^0.1.4",
"to-fast-properties": "^4.0.0",
"to-regex-range": "^5.0.1",
Expand Down

0 comments on commit fe3667b

Please sign in to comment.