Skip to content

Commit

Permalink
test: wikilinks convert
Browse files Browse the repository at this point in the history
Myllaume committed Oct 2, 2024
1 parent c2b28fa commit 9fc1d3f
Showing 3 changed files with 114 additions and 21 deletions.
23 changes: 2 additions & 21 deletions core/models/template.js
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ import app from '../../package.json';
import { isAnImagePath, slugify } from '../utils/misc.js';
import langPck from './lang.js';
import { fileURLToPath } from 'url';
import convertWikilinks from '../utils/convertWikilinks.js';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

@@ -218,27 +219,7 @@ class Template {
return slugify(input);
});
templateEngine.addFilter('convertLinks', (input, opts, idToHighlight) => {
// Replace wikilinks: "[[g:1234567890|toto]]" to "<a>toto</a>"
input = input.replace(Link.regexWikilink, (match, _, type, targetId, __, text) => {
const record = graph.records.find(({ id }) => id === targetId.toLowerCase());

if (!record) return match;

const isNumbers = !isNaN(Number(targetId));

let linkContent;
if (text) {
linkContent = text;
} else if (opts['link_symbol']) {
linkContent = opts['link_symbol'];
} else {
linkContent = isNumbers ? match : targetId;
}

return `<a href="#${record.id}" title="${escapeQuotes(record.title)}" class="record-link ${
record.id === idToHighlight ? 'highlight' : ''
}">${linkContent}</a>`.trim();
});
input = convertWikilinks(input, graph.records, opts, idToHighlight);

if (bibliography) {
Citr.util.extractCitations(input).forEach((quoteText, index) => {
43 changes: 43 additions & 0 deletions core/utils/convertWikilinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const wikilinkRE = new RegExp(/\[\[((?<type>[^:|\]]+?):)?(?<id>.+?)(\|(?<text>.+?))?\]\]/, 'g');

/**
* @param {string} markdown
* @param {Record[]} records
* @param {Config} opts
* @param {string} idToHighlight
* @returns string
* @example
* convertWikilinks('[[g:1234567890|toto]]'); // <a>toto</a>
*/

function convertWikilinks(markdown, records, opts, idToHighlight) {
return markdown.replace(wikilinkRE, (match, _, type, targetId, __, text) => {
const record = records.find(({ id }) => id === targetId.toLowerCase());

if (!record) return match;

let linkLibelle;
if (text) {
linkLibelle = text;
} else if (opts['link_symbol']) {
linkLibelle = opts['link_symbol'];
} else {
const isNumbers = !isNaN(Number(targetId));

linkLibelle = isNumbers ? match : targetId;
}

return `<a href="#${record.id}" title="${escapeQuotes(record.title)}" class="record-link ${record.id === idToHighlight ? 'highlight' : ''}">${linkLibelle}</a>`;
});
}

/**
* @param {string} text
* @returns {string}
*/

function escapeQuotes(text) {
return text.replace(/'/g, '&apos;').replace(/"/g, '&quot;');
}

export default convertWikilinks;
69 changes: 69 additions & 0 deletions core/utils/convertWikilinks.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import convertWikilinks from './convertWikilinks';

describe('convertWikilinks', () => {
const opts = {
link_symbol: '→',
};
const records = [
{
id: '20220403222345',
title: 'Paul Otlet',
},
{
id: 'dewey',
title: 'Dewey',
},
{
id: 'cdu',
title: 'CDU',
},
];

it('should replace one link on text, with symbol', () => {
const markdown = 'Lorem ipsum [[20220403222345]] dolor est.';

expect(convertWikilinks(markdown, records, opts, records[0].id)).toEqual(
`Lorem ipsum <a href="#20220403222345" title="Paul Otlet" class="record-link highlight">→</a> dolor est.`,
);
});

it('should replace one link on text, with link text', () => {
const markdown = 'Lorem ipsum [[20220403222345|Paul Otlet]] dolor est.';

expect(convertWikilinks(markdown, records, opts, records[0].id)).toEqual(
`Lorem ipsum <a href="#20220403222345" title="Paul Otlet" class="record-link highlight">Paul Otlet</a> dolor est.`,
);
});

it('should replace one link on text, with link content', () => {
const markdown = 'Lorem ipsum [[20220403222345]] dolor est.';

expect(convertWikilinks(markdown, records, {}, records[0].id)).toEqual(
`Lorem ipsum <a href="#20220403222345" title="Paul Otlet" class="record-link highlight">[[20220403222345]]</a> dolor est.`,
);
});

it('should replace several link with capitalized text as id', () => {
const markdown = 'Lorem ipsum [[Dewey]] dolor est [[CDU]].';

expect(convertWikilinks(markdown, records, {}, records[0].id)).toEqual(
`Lorem ipsum <a href="#dewey" title="Dewey" class="record-link ">Dewey</a> dolor est <a href="#cdu" title="CDU" class="record-link ">CDU</a>.`,
);
});

it('should not replace link for unknown record id', () => {
const markdown = 'Lorem ipsum [[unknown]] dolor est.';

expect(convertWikilinks(markdown, records, {}, records[0].id)).toEqual(
`Lorem ipsum [[unknown]] dolor est.`,
);
});

it('should not add class "highlight" if not current record', () => {
const markdown = 'Lorem ipsum [[20220403222345]] dolor est.';

expect(convertWikilinks(markdown, records, {}, '20190403222543')).toEqual(
`Lorem ipsum <a href="#20220403222345" title="Paul Otlet" class="record-link ">[[20220403222345]]</a> dolor est.`,
);
});
});

0 comments on commit 9fc1d3f

Please sign in to comment.