-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
basic backlinks implementation, closes #15
- Loading branch information
1 parent
464c72f
commit fe3667b
Showing
7 changed files
with
119 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters