Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix img base64 #159

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/models/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ const minValues = {
attraction_horizontal: 0,
};

function pathExists(path) {
function pathExists(path, helpers) {
if (fs.existsSync(path)) {
return path;
}
Expand Down
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
Loading