1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00

Desktop: Fixes #10466: Fix tables missing source map attributes (#10516)

This commit is contained in:
Henry Heino 2024-05-30 00:34:52 -07:00 committed by GitHub
parent 789d19b18c
commit 418a6e455f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 49 additions and 7 deletions

View File

@ -1291,6 +1291,7 @@ packages/renderer/MdToHtml/rules/mermaid.js
packages/renderer/MdToHtml/rules/sanitize_html.js
packages/renderer/MdToHtml/rules/source_map.js
packages/renderer/MdToHtml/rules/tableHorizontallyScrollable.js
packages/renderer/MdToHtml/rules/utils/defaultRule.js
packages/renderer/MdToHtml/setupLinkify.js
packages/renderer/MdToHtml/validateLinks.js
packages/renderer/assetsToHeaders.js

1
.gitignore vendored
View File

@ -1270,6 +1270,7 @@ packages/renderer/MdToHtml/rules/mermaid.js
packages/renderer/MdToHtml/rules/sanitize_html.js
packages/renderer/MdToHtml/rules/source_map.js
packages/renderer/MdToHtml/rules/tableHorizontallyScrollable.js
packages/renderer/MdToHtml/rules/utils/defaultRule.js
packages/renderer/MdToHtml/setupLinkify.js
packages/renderer/MdToHtml/validateLinks.js
packages/renderer/assetsToHeaders.js

View File

@ -49,6 +49,8 @@ describe('MdToHtml', () => {
checkboxRenderingType: 2,
},
};
} else if (mdFilename.startsWith('sourcemap_')) {
mdToHtmlOptions.mapsToLine = true;
}
const markdown = await shim.fsDriver().readFile(mdFilePath);

View File

@ -0,0 +1,21 @@
<div class="joplin-table-wrapper">
<table class="maps-to-line" source-line="0" source-line-end="3">
<thead>
<tr>
<th>This</th>
<th>is</th>
<th>a</th>
<th>test</th>
</tr>
</thead>
<tbody>
<tr>
<td>This</td>
<td>table</td>
<td>has</td>
<td>line numbers</td>
</tr>
</tbody>
</table>
</div>
<p class="maps-to-line" source-line="4" source-line-end="5">When <code class="inline-code">mapsToLine</code> is enabled for this file, the table above should have line numbers that link to the original markdown.</p>

View File

@ -0,0 +1,5 @@
| This | is | a | test |
|------|----|---|------|
| This | table | has | line numbers |
When `mapsToLine` is enabled for this file, the table above should have line numbers that link to the original markdown.

View File

@ -1,18 +1,21 @@
import * as MarkdownIt from 'markdown-it';
import type * as MarkdownIt from 'markdown-it';
import type * as Renderer from 'markdown-it/lib/renderer';
import defaultRule from './utils/defaultRule';
export default {
// Make table horizontally scrollable by give table a div parent, so the width of the table is limited to the screen width.
// see https://github.com/laurent22/joplin/pull/10161
plugin: (markdownIt: MarkdownIt) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
markdownIt.renderer.rules.table_open = function(tokens: MarkdownIt.Token[], idx: number, options: MarkdownIt.Options, _env: any, renderer: any) {
const current = renderer.renderToken(tokens, idx, options);
const defaultTableOpen = defaultRule(markdownIt, 'table_open');
markdownIt.renderer.rules.table_open = function(tokens: MarkdownIt.Token[], idx: number, options: MarkdownIt.Options, env: unknown, renderer: Renderer) {
const current = defaultTableOpen(tokens, idx, options, env, renderer);
// joplin-table-wrapper css is set in packages/renderer/noteStyle.ts
return `<div class="joplin-table-wrapper">\n${current}`;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
markdownIt.renderer.rules.table_close = function(tokens: MarkdownIt.Token[], idx: number, options: MarkdownIt.Options, _env: any, renderer: any) {
const current = renderer.renderToken(tokens, idx, options);
const defaultTableClose = defaultRule(markdownIt, 'table_close');
markdownIt.renderer.rules.table_close = function(tokens: MarkdownIt.Token[], idx: number, options: MarkdownIt.Options, env: unknown, renderer: Renderer) {
const current = defaultTableClose(tokens, idx, options, env, renderer);
return `${current}</div>\n`;
};
},

View File

@ -0,0 +1,9 @@
import type * as MarkdownIt from 'markdown-it';
import type * as Renderer from 'markdown-it/lib/renderer';
const defaultRule = (markdownIt: MarkdownIt, key: keyof Renderer.RenderRuleRecord): Renderer.RenderRule => {
if (markdownIt.renderer.rules[key]) return markdownIt.renderer.rules[key];
return (tokens, idx, options, _env, renderer) => renderer.renderToken(tokens, idx, options);
};
export default defaultRule;