Skip to content

Commit

Permalink
feat: slugify ids
Browse files Browse the repository at this point in the history
  • Loading branch information
Myllaume committed Dec 2, 2024
1 parent 21dbcd0 commit ec7c134
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 43 deletions.
32 changes: 13 additions & 19 deletions core/models/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,12 @@ export default class Record {
static recordFromFile(file, config) {
const { content, head } = readYmlFm(file, { schema: 'failsafe' });

const links = parseWikilinks(content, config);

const props = {
...normalizeInput(head, config),
links,
links: parseWikilinks(content, config),
content,
};

if (typeof props.types === 'string') {
props.types = [props.types];
}
if (typeof props.tags === 'string') {
props.tags = [props.tags];
}
if (typeof props.begin === 'string') {
props.begin = new Date(props.begin).getTime() / 1000;
}
if (typeof props.end === 'string') {
props.end = new Date(props.end).getTime() / 1000;
}

const { error } = schema.validate(props);
if (error) {
throw new Error(`Record contains error: ${error.message}`);
Expand All @@ -100,7 +85,7 @@ export default class Record {
id: props.id,
title: props.title,
content: props.content,
links,
links: props.links,
tags: props.tags,
types: props.types,
begin: props.begin,
Expand Down Expand Up @@ -141,8 +126,6 @@ export default class Record {
},
config,
);

return undefined;
}

/**
Expand All @@ -154,6 +137,10 @@ export default class Record {
static recordFromCiteItem(citeItem, config, bibliography) {
const libraryItem = bibliography.library[citeItem.id];

if (!libraryItem) {
throw new Error(`Library item "${citeItem.id}" is unknown`);
}

const props = {
id: citeItem.id,
title: libraryItem['title'],
Expand Down Expand Up @@ -318,6 +305,9 @@ function normalizeInput(head, config) {
...normalizedHead,
};

if (!props.id && props.title) {
props.id = props.title;
}
if (typeof props.types === 'string') {
props.types = [props.types];
}
Expand All @@ -331,6 +321,10 @@ function normalizeInput(head, config) {
props.end = new Date(props.end).getTime() / 1000;
}

if (props.id) {
props.id = slugify(props.id);
}

if (props.types) {
const knownTypes = config.getTypesRecords();
props.types = props.types.reduce((acc, curr, i, arr) => {
Expand Down
44 changes: 23 additions & 21 deletions core/models/record.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,6 @@ describe('Record model', () => {
expect(record.thumbnail).toBeUndefined();
});

it('should generate the correct YAML front matter', () => {
const record = new Record(props, config);

const file = record.getFileContent(true);

expect(file).toEqual(`---
id: "20200501150208"
title: Test Record
types:
- type1
- type2
tags:
- tag1
- tag2
thumbnail: img.jpg
author: John Doe
---
This is a test content`);
});

it('should generate YAML front matter with id', () => {
const record = new Record({ ...props, tags: undefined }, config);

Expand Down Expand Up @@ -164,6 +143,29 @@ File linked to [[20210901132906]]`;
});
});

it('should title became id if no id', () => {
const file = `---
title: Test Record
---
Content`;

const result = Record.recordFromFile(file, config);
expect(result).toEqual({
id: 'test-record',
title: 'Test Record',
content: '\n\nContent',
links: [],
types: ['undefined'],
tags: [],
metas: {},
begin: undefined,
end: undefined,
thumbnail: undefined,
config: config,
});
});

it('should get from citeproc item', () => {
/** @type {import('../utils/citeExtractor.js'.CiteItem)} */
const citeItem = {
Expand Down
4 changes: 3 additions & 1 deletion core/utils/convertWikilinks.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import slugify from './slugify';

const wikilinkRE = new RegExp(/\[\[((?<type>[^:|\]]+?):)?(?<id>.+?)(\|(?<text>.+?))?\]\]/, 'g');

/**
Expand All @@ -12,7 +14,7 @@ const wikilinkRE = new RegExp(/\[\[((?<type>[^:|\]]+?):)?(?<id>.+?)(\|(?<text>.+

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

if (!record) return match;

Expand Down
5 changes: 3 additions & 2 deletions core/utils/parseWikilinks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import extractParaphs from './paraphExtractor';
import slugify from './slugify';

/**
* @typedef Link
Expand Down Expand Up @@ -27,7 +28,7 @@ export default function parseWikilinks(markdown, config) {

for (const match of markdown.matchAll(wikilinkRE) || []) {
const [full, _, type, id, __, placeholder] = match;
const target = id.toLowerCase();
const target = slugify(id);

linksDict.set(target, {
type: type || 'undefined',
Expand All @@ -40,7 +41,7 @@ export default function parseWikilinks(markdown, config) {
extractParaphs(markdown).forEach((paraph) => {
for (const match of paraph.matchAll(wikilinkRE)) {
const [full, _, type, id] = match;
const target = id.toLowerCase();
const target = slugify(id);

linksDict.get(target).contexts.add(paraph);
}
Expand Down

0 comments on commit ec7c134

Please sign in to comment.