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

Another bug about #2733 #3487

Closed
linuxbckp opened this issue Oct 13, 2024 · 8 comments
Closed

Another bug about #2733 #3487

linuxbckp opened this issue Oct 13, 2024 · 8 comments

Comments

@linuxbckp
Copy link
Contributor

Marked version:
14.1.2
Describe the bug
In #2733, someone gave out a function walkTokens(token). But this workaroound has another bug, it will repeatedly change URL.

To Reproduce
For example, if a file is ./d.efg located at C:/abc/,
if I opened ./d.efg once, the result will become C:/abc/d.efg (correct),
if I opened ./d.efg twice, the result will become C:/abc/C:/abc/d.efg (incorrect).

Expected behavior
marker.use() ( here is marker.use( { walkTokens(token) } ) ) should be non-state.

@UziTech
Copy link
Member

UziTech commented Oct 13, 2024

Can you create a repo that can reproduce this?

I don't know what you mean by "opened ./d.efg twice" marked does not do anything different if you open a file.

@linuxbckp
Copy link
Contributor Author

Here is a repo: https://github.com/CNOCTAVE/marked_reader
Press Ctrl + Shift + i to open Electron console, then you can see errors.
Click the left top button, choose a Markdown file. Do it twice, then reproduce the errors.

@linuxbckp
Copy link
Contributor Author

In Markdown, ./-style relative path is common. When in Electron(a Node.js-based framework), Electron always parse ./ path as Node path, so ./ path should be rewritten to absolute path. This repo is a Markdown reader, user should open a local Markdown file. To make that bug clearer, you can write a picture path as relative path, if picture cannot correctly shows out, that's bug!

@linuxbckp
Copy link
Contributor Author

linuxbckp commented Oct 14, 2024

Like:
New a xxx.md, write:

d

d.png

where this xxx.md and ./d.png is at C:/abc/

@UziTech
Copy link
Member

UziTech commented Oct 14, 2024

You are adding the walkTokens function everytime they open a file. You should only be adding it once:

import { marked } from "marked";
function walkTokens(token) {
    if (token.type === 'image' || token.type === 'link') {
        token.href = markdownDir + ("/" + token.href).replace("//", "/").replace("\\", "/").replace("%5C", "/").replace("./", "/"); // 修正相对路径
    }
}
marked.use({ walkTokens });

or you can use an instance to not have the extensions be global

import { Marked } from "marked";
...
ipcMain.on('render-markdown', (event, filePath) => {
    ...
    function walkTokens(token) {
        if (token.type === 'image' || token.type === 'link') {
            token.href = markdownDir + ("/" + token.href).replace("//", "/").replace("\\", "/").replace("%5C", "/").replace("./", "/"); // 修正相对路径
        }
    }
    const marked = new Marked();
    marked.use({ walkTokens });
    const htmlContent = marked.parse(markdownContent);
    event.reply('markdown-rendered', htmlContent);
});

@linuxbckp
Copy link
Contributor Author

linuxbckp commented Oct 14, 2024

Can you PR to https://github.com/CNOCTAVE/marked_reader to show your modified code? It doesn't work for me.
image

@UziTech
Copy link
Member

UziTech commented Oct 14, 2024

use marked.parse(...) not marked(...) still works sometimes but is not the recommended way to run markdown through marked.

@linuxbckp
Copy link
Contributor Author

Now the bug is gone after using marked.parse(...).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants