Skip to content

Commit

Permalink
fix: Better handling of source map for importing files (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
shd101wyy authored Oct 15, 2023
1 parent dfffe53 commit 377c174
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 92 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Please visit https://github.com/shd101wyy/vscode-markdown-preview-enhanced/relea

## [Unreleased]

## [0.9.3] - 2023-10-15

### Bug fixes

- Better handling of source map for importing files.

## [0.9.2] - 2023-10-15

### New features
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "crossnote",
"version": "0.9.2",
"version": "0.9.3",
"description": "A powerful markdown notebook tool",
"keywords": [
"markdown"
Expand Down
125 changes: 39 additions & 86 deletions src/markdown-engine/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export interface TransformMarkdownOptions {
headingIdGenerator?: HeadingIdGenerator;
notebook: Notebook;
forJest?: boolean;
timestamp?: number;
}

const fileExtensionToLanguageMap = {
Expand Down Expand Up @@ -245,6 +246,7 @@ export async function transformMarkdown(
notebook,
forJest = false,
fileHash,
timestamp,
}: TransformMarkdownOptions,
): Promise<TransformMarkdownOutput> {
// Replace CRLF with LF
Expand Down Expand Up @@ -593,7 +595,7 @@ export async function transformMarkdown(
// =========== Start: File import ============
const importMatch = line.match(/^(\s*)@import(\s+)"([^"]+)";?/);
const imageImportMatch = line.match(
/^(\s*)!\[([^\]]*)\]\(([^)]+)\)(?:{([^}]*)})?\s*$/,
/^(\s*)!\[([^\]]*)\]\(([^)]+)\)(?:{([^}]*)})?(\s*)$/,
);
const wikilinkImportMatch = line.match(
/^(\s*)!\[\[(.+?)\]\](?:{([^}]*)})?\s*$/,
Expand Down Expand Up @@ -626,6 +628,9 @@ export async function transformMarkdown(
}
}
}
if (canCreateAnchor()) {
config['data-source-line'] = lineNo + 1;
}

let absoluteFilePath: string;
if (
Expand All @@ -648,14 +653,12 @@ export async function transformMarkdown(
const extname = path.extname(absoluteFilePath).toLocaleLowerCase();
let output = '';
if (filePath === '[TOC]') {
if (!config) {
config = {
// same case as in normalized attributes
['depth_from']: 1,
['depth_to']: 6,
['ordered_list']: true,
};
}
/*
// NOTE: No need to set this
config['depth_from'] = config['depth_from'] ?? 1;
config['depth_to'] = config['depth_to'] ?? 6;
config['ordered_list'] = config['ordered_list'] ?? true;
*/
config['cmd'] = 'toc';
config['hide'] = true;
config['run_on_save'] = true;
Expand Down Expand Up @@ -693,51 +696,42 @@ export async function transformMarkdown(
imageSrc =
path.relative(fileDirectoryPath, absoluteFilePath) +
'?' +
Math.random();
(timestamp ?? Math.random());
} else {
imageSrc =
'/' +
path.relative(projectDirectoryPath, absoluteFilePath) +
'?' +
Math.random();
(timestamp ?? Math.random());
}
// enchodeURI(imageSrc) is wrong. It will cause issue on Windows
// #414: https://github.com/shd101wyy/markdown-preview-enhanced/issues/414
imageSrc = imageSrc.replace(/ /g, '%20').replace(/\\/g, '/');
filesCache[filePath] = imageSrc;
}

if (config) {
if (
config['width'] ||
config['height'] ||
config['class'] ||
config['id']
) {
output = `<img src="${imageSrc}" `;
for (const key in config) {
// eslint-disable-next-line no-prototype-builtins
if (config.hasOwnProperty(key)) {
output += ` ${key}="${config[key]}" `;
}
}
output += '>';
} else {
output = '![';
if (config['alt']) {
output += config['alt'];
}
output += `](${imageSrc}`;
if (config['title']) {
output += ` "${config['title']}"`;
}
output += ') ';
}
output = '![';
if (config['alt']) {
output += config['alt'];
delete config['alt'];
}
output += `](${imageSrc}`;
if (config['title']) {
output += ` "${config['title']}"`;
delete config['title'];
}
output += ')';
const configStr = stringifyBlockAttributes(config);
if (configStr) {
output += `{${configStr}} `;
} else {
output = `![](${imageSrc}) `;
output += ' ';
}
} else if (imageImportMatch) {
output = imageImportMatch[0]; // NOTE: Don't change anything here
const configStr = stringifyBlockAttributes(config);
output = `![${imageImportMatch[2] ?? ''}](${
imageImportMatch[3] ?? ''
})${configStr ? `{${configStr}}` : ''}${imageImportMatch[5]}`;
}
i = end + 1;
lineNo = lineNo + 1;
Expand All @@ -756,7 +750,7 @@ export async function transformMarkdown(
);
filesCache[absoluteFilePath] = fileContent;

if (config && (config['line_begin'] || config['line_end'])) {
if (config['line_begin'] || config['line_end']) {
const lines = fileContent.split(/\n/);
fileContent = lines
.slice(
Expand All @@ -766,7 +760,7 @@ export async function transformMarkdown(
.join('\n');
}

if (config && config['code_block']) {
if (config['code_block']) {
const fileExtension = extname.slice(1, extname.length);
output = `\`\`\`${
config['as'] ||
Expand All @@ -775,7 +769,7 @@ export async function transformMarkdown(
} ${stringifyBlockAttributes(
config,
)} \n${fileContent}\n\`\`\` `;
} else if (config && config['cmd']) {
} else if (config['cmd']) {
if (!config['id']) {
// create `id` for code chunk
config['id'] = computeChecksum(absoluteFilePath);
Expand Down Expand Up @@ -880,7 +874,7 @@ export async function transformMarkdown(
// css or less file
output = `<style>${fileContent}</style>`;
} else if (extname === '.pdf') {
if (config && config['page_no']) {
if (config['page_no']) {
// only disply the nth page. 1-indexed
const pages = fileContent.split('\n');
let pageNo = parseInt(config['page_no'], 10) - 1;
Expand Down Expand Up @@ -943,11 +937,8 @@ export async function transformMarkdown(
output = "<script>${fileContent}</script>"
*/
// # codeblock
let aS = null;
if (config) {
aS = config['as'];
}
if (config && config['code_block'] === false) {
const aS = config['as'] ?? null;
if (config['code_block'] === false) {
// https://github.com/shd101wyy/markdown-preview-enhanced/issues/916
output = fileContent;
} else {
Expand Down Expand Up @@ -1001,44 +992,6 @@ export async function transformMarkdown(
// =========== End: File import ============
// =========== Start: Normal line ============
else {
// =========== Start: Add attributes to links and images ========
if (canCreateAnchor()) {
let newLine = '';
let restLine = line;
const regexp = /!?\[([^\]]*)\]\(([^)]*)\)/;
// Add and data-source-line to links and images {...} attributes
// eslint-disable-next-line no-constant-condition
while (true) {
const match = restLine.match(regexp);
if (!match || typeof match.index !== 'number') {
newLine = newLine + restLine;
break;
} else {
newLine =
newLine + restLine.substring(0, match.index + match[0].length);
restLine = restLine.substring(match.index + match[0].length);

if (restLine[0] === '{') {
// Might find attribute
// TODO: Write a generic parser for this
const end = restLine.indexOf('}');
if (end > 0) {
const attributeString = restLine.substring(1, end);
newLine += `{data-source-line="${
lineNo + 1
}" ${attributeString}}`;
restLine = restLine.substring(end + 1);
}
} else {
newLine += `{data-source-line="${lineNo + 1}"}`;
}
}
}
line = newLine;
}

// =========== End: Add attributes to links and images ========

i = end + 1;
lineNo = lineNo + 1;
outputString = outputString + line + '\n';
Expand Down
6 changes: 3 additions & 3 deletions test/markdown/test-files/test2.expect.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
```text cmd="toc" depthFrom=1 depthTo=6 orderedList=false hide=true run_on_save=true modify_source=true code_chunk_offset=0
```text cmd="toc" depthFrom=1 depthTo=6 orderedList=false data-source-line=1 hide=true run_on_save=true modify_source=true code_chunk_offset=0
```


- [Heading 1](#heading-1){data-source-line="5"}
- [Heading 2](#heading-2){data-source-line="6"}
- [Heading 1](#heading-1)
- [Heading 2](#heading-2)



Expand Down
2 changes: 1 addition & 1 deletion test/markdown/test-files/test3.expect.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# This is test3.md {#this-is-test3md data-source-line="1"}

![@embedding](test2.md){x=1 y=2 embedding="JTBBJTYwJTYwJTYwdGV4dCUyMGNtZCUzRCUyMnRvYyUyMiUyMGRlcHRoRnJvbSUzRDElMjBkZXB0aFRvJTNENiUyMG9yZGVyZWRMaXN0JTNEZmFsc2UlMjBoaWRlJTNEdHJ1ZSUyMHJ1bl9vbl9zYXZlJTNEdHJ1ZSUyMG1vZGlmeV9zb3VyY2UlM0R0cnVlJTIwJTIwJTBBJTYwJTYwJTYwJTIwJTIwJTBBJTBBJTBBLSUyMCU1QkhlYWRpbmclMjAxJTVEKCUyM2hlYWRpbmctMSklMEElMjAlMjAtJTIwJTVCSGVhZGluZyUyMDIlNUQoJTIzaGVhZGluZy0yKSUwQSUwQSUwQSUwQSUyMyUyMEhlYWRpbmclMjAxJTIwJTdCJTIzaGVhZGluZy0xJTIwJTdEJTBBJTBBcGFyYWdyYXBoJTBBJTBBJTIzJTIzJTIwSGVhZGluZyUyMDIlMjAlN0IlMjNoZWFkaW5nLTIlMjAlN0QlMEElMEFwYXJhZ3JhcGglMEElMjAlMjA="}
![@embedding](test2.md){x=1 y=2 data-source-line=3 embedding="JTBBJTYwJTYwJTYwdGV4dCUyMGNtZCUzRCUyMnRvYyUyMiUyMGRlcHRoRnJvbSUzRDElMjBkZXB0aFRvJTNENiUyMG9yZGVyZWRMaXN0JTNEZmFsc2UlMjBoaWRlJTNEdHJ1ZSUyMHJ1bl9vbl9zYXZlJTNEdHJ1ZSUyMG1vZGlmeV9zb3VyY2UlM0R0cnVlJTIwJTIwJTBBJTYwJTYwJTYwJTIwJTIwJTBBJTBBJTBBLSUyMCU1QkhlYWRpbmclMjAxJTVEKCUyM2hlYWRpbmctMSklMEElMjAlMjAtJTIwJTVCSGVhZGluZyUyMDIlNUQoJTIzaGVhZGluZy0yKSUwQSUwQSUwQSUwQSUyMyUyMEhlYWRpbmclMjAxJTIwJTdCJTIzaGVhZGluZy0xJTIwJTdEJTBBJTBBcGFyYWdyYXBoJTBBJTBBJTIzJTIzJTIwSGVhZGluZyUyMDIlMjAlN0IlMjNoZWFkaW5nLTIlMjAlN0QlMEElMEFwYXJhZ3JhcGglMEElMjAlMjA="}
24 changes: 24 additions & 0 deletions test/markdown/test-files/test4.expect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
`[test](test.md)`
**[test](test.md)**

`![test](test.png)`
**![test](test.md)**

![test](test.png){data-source-line=7}
![test](https://test.png){data-source-line=8}
![test](test.png#hash){data-source-line=9}
![This is alt](test.png "This is title"){data-source-line=10}
![test](test.png){title="This is title" alt="This is alt" data-source-line=11}
![test](test.png){x=1 y=2 data-source-line=12}

![](/test.png?12345){data-source-line=14}
![](https:/test.png){data-source-line=15}
![](/test.png?12345){data-source-line=16}
![This is alt](/test.png?12345 "This is title"){data-source-line=17}
![](/test.png?12345){x=1 y=2 data-source-line=18}

![](/test.png?12345){data-source-line=20}
![](https://test.png){data-source-line=21}
![](/test.png?12345){data-source-line=22}
![This is alt](/test.png?12345 "This is title"){data-source-line=23}
![](/test.png?12345){x=1 y=2 data-source-line=24}
24 changes: 24 additions & 0 deletions test/markdown/test-files/test4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
`[test](test.md)`
**[test](test.md)**

`![test](test.png)`
**![test](test.md)**

![test](test.png)
![test](https://test.png)
![test](test.png#hash)
![This is alt](test.png "This is title")
![test](test.png){title="This is title" alt="This is alt"}
![test](test.png){x=1 y=2}

![[test.png]]
![[https://test.png]]
![[test.png#hash]]
![[test.png]]{title="This is title" alt="This is alt"}
![[test.png]]{x=1 y=2}

@import "test.png"
@import "https://test.png"
@import "test.png#hash"
@import "test.png" {title="This is title" alt="This is alt"}
@import "test.png" {x=1 y=2}
3 changes: 2 additions & 1 deletion test/markdown/transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ describe('test markdown transformer', () => {
projectDirectoryPath: path.join(__dirname, './test-files'),
filesCache: {},
useRelativeFilePath: false,
protocolsWhiteListRegExp: null,
protocolsWhiteListRegExp: /^(https?)/,
forJest: true,
timestamp: 12345,
});
};

Expand Down

0 comments on commit 377c174

Please sign in to comment.