-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
114 additions
and
21 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
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 |
---|---|---|
@@ -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, ''').replace(/"/g, '"'); | ||
} | ||
|
||
export default convertWikilinks; |
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 |
---|---|---|
@@ -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.`, | ||
); | ||
}); | ||
}); |