Skip to content

Commit

Permalink
test: thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
Myllaume committed Dec 13, 2024
1 parent 5a39fd2 commit 719320a
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 177 deletions.
74 changes: 40 additions & 34 deletions core/models/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ import GraphEngine from 'graphology';
import { extent } from 'd3';
import extractCitations from '../utils/citeExtractor.js';

/**
* @typedef ThumbnailIntegration
* @type {object}
* @property {string} name
* @property {string} path
*/

const md = new mdIt({
html: true,
linkify: true,
Expand Down Expand Up @@ -187,44 +194,45 @@ class Template {
});
}

/**
* @param {string} type
* @param {import('./config.js').Options} opts
* @returns {'image'|'color'}
*/
/** @type {Map<string, ThumbnailIntegration>} */
const thumbnailsMap = new Map();

function getFormatOfTypeRecord(type, opts) {
if (this.config.opts.images_origin) {
const validExtnames = new Set(['.jpg', '.jpeg', '.png']);

const { fill } = opts['record_types'][type];
if (validExtnames.has(path.extname(fill))) {
return 'image';
}
return 'color';
}

const thumbnailsFromTypesRecords = Array.from(this.config.getTypesRecords())
.filter((type) => getFormatOfTypeRecord(type, this.config.opts) === 'image')
.map((type) => {
return {
name: recordTypes[type]['fill'],
path: path.join(imagesPath, recordTypes[type]['fill']),
};
records.forEach((record) => {
if (
record.thumbnail &&
validExtnames.has(path.extname(record.thumbnail)) &&
fs.existsSync(path.join(this.config.opts.images_origin, record.thumbnail))
) {
thumbnailsMap.set(record.thumbnail, {
name: record.thumbnail,
path: path.join(this.config.opts.images_origin, record.thumbnail),
});
}
});
const thumbnailsFromRecords = [...records.values()]
.filter(({ thumbnail }) => typeof thumbnail === 'string')
.map(({ thumbnail }) => {
return {
name: thumbnail,
path: path.join(imagesPath, thumbnail),
};

Object.entries(this.config.opts.record_types).forEach(([type, { fill }]) => {
if (
validExtnames.has(path.extname(fill)) &&
fs.existsSync(path.join(this.config.opts.images_origin, fill))
) {
thumbnailsMap.set(fill, {
name: fill,
path: path.join(this.config.opts.images_origin, fill),
});
}
});
}

const templateEngine = new nunjucks.Environment();

md.inline.ruler2.push('image_to_base64', (state) =>
Template.mdItImageToBase64(imagesPath, state),
);
if (imagesPath) {
md.inline.ruler2.push('image_to_base64', (state) =>
Template.mdItImageToBase64(imagesPath, state),
);
}

templateEngine.addFilter('slugify', (input) => {
return slugify(input);
Expand Down Expand Up @@ -298,7 +306,7 @@ class Template {
...rest,
backlinks,
links: toto,
thumbnail: !!thumbnail ? path.join(imagesPath, thumbnail) : undefined,
thumbnail: thumbnailsMap.has(thumbnail) ? thumbnailsMap.get(thumbnail).path : undefined,
};
}),

Expand Down Expand Up @@ -339,9 +347,7 @@ class Template {
keywords,
},

nodeThumbnails: [...thumbnailsFromTypesRecords, ...thumbnailsFromRecords].filter(({ path }) =>
isAnImagePath(path),
),
nodeThumbnails: [...thumbnailsMap.values()],

focusIsActive: !(focusMax <= 0),

Expand Down
8 changes: 3 additions & 5 deletions e2e/csv/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ nodes_origin: ./nodes.csv
links_origin: ./links.csv
nodes_online: null
links_online: null
images_origin: null
images_origin: ../records
export_target: ../../temp
history: false
focus_max: 2
Expand All @@ -14,7 +14,7 @@ record_types:
fill: "#eeeeee"
Personne:
stroke: "#1b9e77"
fill: "#1b9e77"
fill: people.png
Œuvre:
stroke: "#d95f02"
fill: "#d95f02"
Expand Down Expand Up @@ -45,7 +45,7 @@ graph_highlight_on_hover: true
graph_text_size: 7
graph_arrows: true
node_size_method: unique
node_size: 5
node_size: 10
node_size_max: 20
node_size_min: 10
attraction_force: 600
Expand All @@ -55,8 +55,6 @@ attraction_horizontal: 0.1
hide_id_from_record_header: false
views: {}
record_metas:
- prenom
- nom
- titre
- pays
- domaine
Expand Down
Loading

0 comments on commit 719320a

Please sign in to comment.