1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-18 09:35:20 +02:00

Desktop: Added support for rendered note metadata, in particular the joplin-metadata-print-title tag

This commit is contained in:
Laurent Cozic 2024-12-10 17:13:08 +01:00
parent d1fc69ffbe
commit 5d84f80ad1
6 changed files with 80 additions and 1 deletions

View File

@ -1195,6 +1195,8 @@ packages/lib/services/interop/InteropService_Importer_Raw.js
packages/lib/services/interop/Module.test.js
packages/lib/services/interop/Module.js
packages/lib/services/interop/types.js
packages/lib/services/interop/utils.test.js
packages/lib/services/interop/utils.js
packages/lib/services/joplinCloudUtils.js
packages/lib/services/joplinServer/personalizedUserContentBaseUrl.js
packages/lib/services/keychain/KeychainService.test.js

2
.gitignore vendored
View File

@ -1171,6 +1171,8 @@ packages/lib/services/interop/InteropService_Importer_Raw.js
packages/lib/services/interop/Module.test.js
packages/lib/services/interop/Module.js
packages/lib/services/interop/types.js
packages/lib/services/interop/utils.test.js
packages/lib/services/interop/utils.js
packages/lib/services/joplinCloudUtils.js
packages/lib/services/joplinServer/personalizedUserContentBaseUrl.js
packages/lib/services/keychain/KeychainService.test.js

View File

@ -16,6 +16,7 @@ import { assetsToHeaders } from '@joplin/renderer';
import getPluginSettingValue from '../plugins/utils/getPluginSettingValue';
import { LinkRenderingType } from '@joplin/renderer/MdToHtml';
import Logger from '@joplin/utils/Logger';
import { parseRenderedNoteMetadata } from './utils';
const logger = Logger.create('InteropService_Exporter_Html');
@ -128,8 +129,11 @@ export default class InteropService_Exporter_Html extends InteropService_Exporte
},
},
});
const noteContent = [];
if (item.title) noteContent.push(`<div class="exported-note-title">${escapeHtml(item.title)}</div>`);
const metadata = parseRenderedNoteMetadata(result.html ? result.html : '');
if (!metadata.printTitle) logger.info('Not printing title because joplin-metadata-print-title tag is set to false');
if (metadata.printTitle && item.title) noteContent.push(`<div class="exported-note-title">${escapeHtml(item.title)}</div>`);
if (result.html) noteContent.push(result.html);
const libRootPath = dirname(dirname(__dirname));

View File

@ -0,0 +1,35 @@
import { RenderedNoteMetadata, parseRenderedNoteMetadata } from './utils';
describe('interop/utils', () => {
test.each<[string, RenderedNoteMetadata]>([
[
'',
{ printTitle: true },
],
[
'<!-- joplin-metadata-print-title = false -->',
{ printTitle: false },
],
[
'<!-- joplin-metadata-print-title = true -->',
{ printTitle: true },
],
[
'<!-- joplin-metadata-print-title = 0 -->',
{ printTitle: false },
],
[
'<!-- joplin-metadata-print-title = 1 -->',
{ printTitle: true },
],
[
'<!-- joplin-metadata-print-title = 0 -->',
{ printTitle: false },
],
])('should parse metadata from the note HTML body', async (bodyHtml, expected) => {
const actual = parseRenderedNoteMetadata(bodyHtml);
expect(actual).toEqual(expected);
});
});

View File

@ -0,0 +1,25 @@
/* eslint-disable import/prefer-default-export */
export interface RenderedNoteMetadata {
printTitle: boolean;
}
export const parseRenderedNoteMetadata = (noteHtml: string) => {
const output: RenderedNoteMetadata = {
printTitle: true,
};
// <!-- joplin-metadata-print-title = false -->
const match = noteHtml.match(/<!--[\s]+joplin-metadata-(.*?)[\s]+=[\s]+(.*?)[\s]+-->/);
if (match) {
const [, propName, propValue] = match;
if (propName === 'print-title') {
output.printTitle = propValue.toLowerCase() === 'true' || propValue === '1';
} else {
throw new Error(`Unknown view metadata: ${propName}`);
}
}
return output;
};

View File

@ -0,0 +1,11 @@
# Rendered note metadata
Joplin allows the use of certain metadata tags within the rendered (HTML) version of a note.
At present, the "print-title" metadata tag is the only one supported. By default, the title of a note is displayed at the top when the note is printed or exported. However, this behavior may not always be desired, such as when a plugin needs to produce a document with a specific custom header. In such cases, you can disable the default behavior by including this tag and setting its value to `false`.
You can add this tag either directly in the Markdown document or through a Markdown-it content script in the rendered note.
To prevent the note title from being printed, include the following line in the document:
`<!-- joplin-metadata-print-title = false -->`