Browse Source

Begin to add wikilink support to parser

master
buttercat1791 2 years ago
parent
commit
824e39ecd5
  1. 34
      src/lib/parser.ts

34
src/lib/parser.ts

@ -357,7 +357,8 @@ export default class Pharos extends Asciidoctor { @@ -357,7 +357,8 @@ export default class Pharos extends Asciidoctor {
event.content = content!;
event.tags = [
['title', title!],
['#d', nodeId]
['#d', nodeId],
...this.extractAndNormalizeWikilinks(content!),
];
event.created_at = Date.now();
event.pubkey = pubkey;
@ -371,4 +372,35 @@ export default class Pharos extends Asciidoctor { @@ -371,4 +372,35 @@ export default class Pharos extends Asciidoctor {
}
// #endregion
// #region Utility Functions
/**
* Normalizes a string to lower-kebab-case.
* @param input The string to normalize.
* @returns The normalized string.
*/
private normalizeDTag(input: string): string {
return input
.toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with hyphens.
.replace(/[^a-z0-9\-]/g, ''); // Remove non-alphanumeric characters except hyphens.
}
private extractAndNormalizeWikilinks(content: string): string[][] {
const wikilinkPattern = /\[\[([^\]]+)\]\]/g;
const wikilinks: string[][] = [];
let match: RegExpExecArray | null;
// TODO: Match custom-named wikilinks as defined in NIP-54.
while ((match = wikilinkPattern.exec(content)) !== null) {
const linkName = match[1];
const normalizedText = this.normalizeDTag(linkName);
wikilinks.push(['wikilink', normalizedText]);
}
return wikilinks;
}
// #endregion
}
Loading…
Cancel
Save