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

feat: marked extensions #230

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

feat: marked extensions #230

wants to merge 8 commits into from

Conversation

nobkd
Copy link
Collaborator

@nobkd nobkd commented Mar 14, 2024

WIP/POC implementation for marked extension loading.

Description

The default file to load from is marked.config.js but this can be changed using the marked_config key in site.yaml.
The provided file path currently has to be relative to the root folder of the project / the root passed to nue command

The config js file has to be of format:

// e.g. imports

export default [ // has to be an array
  // initialize marked extensions with functions / own objects here
]

If no file is found, the import() function silently returns an empty object which, on unpacking, results in an empty list for marked_extensions variable

TODO

  • exclude marked config file from output folder
  • add tests

Notes

Maybe allow additional renderers with the following?

marked.use({
 extensions: [
   //...
 ]
})

@tipiirai
Copy link
Contributor

Seems legit. This should only cover extensions that are not possible to implement as Nuemark extensions/tags. Footnote is probably the only one. Anything else?

@nobkd
Copy link
Collaborator Author

nobkd commented Mar 24, 2024

Well, you can also add all the other marked extensions.
It is sometimes easier to just use an existing package instead of re-building stuff yourself.

e.g.

  • extended tables syntax
  • footnotes, smartypants (lite) (as it was removed from the base package)
  • katex could also be interesting for some use cases
  • etc.

https://marked.js.org/using_advanced#extensions


Still WIP, as I have to do the todos, and want to use joinRootPath (or changed name) to import the file.


BTW, I think, this config isn't needed, as the options are deprecated (smartypants, headerIds, mangle) or nonexistent (smartLists at least it's not in this table):
https://github.com/nuejs/nue/pull/230/files#diff-9aaf075b4e5c776d59c27ef6c62e9c1079673f706482643126f348d7c6550cdfR9-R14

@nobkd
Copy link
Collaborator Author

nobkd commented Mar 24, 2024

Rebased on #232 and removed deprecated options. Still have the todo's open...

Edit:
Notes:

  • Seems like the path for import has to be absolute, as it probably tries to load relative to the current js file (just an assumption) / packages from node_modules folder
  • I'm not really sure, how to append the marked config file to the list of files to be excluded from dist, because
    1. I don't quite know the location in code where this has to be added
    2. I have an absolute file path for importing, which could theoretically be absolute from the start

@nobkd
Copy link
Collaborator Author

nobkd commented Apr 3, 2024

I've reached the conclusion, that for now, I'll just define the config file name to marked.config.js, as I am not too sure, how to add a dynamic relative or absolute path to the IGNORE field properly.
If you are against my current concept, please let me know.

(An alternative I thought of was to create a nue.config.js or similar, where multiple config keys are included, that might need some js import. But that's just a thought experiment)

The current loading variant for marked extensions also does not allow changing the marked extensions at runtime, but I don't think that's a big problem.


BTW, I think, this config isn't needed, as the options are deprecated (smartypants, headerIds, mangle) or nonexistent (smartLists at least it's not in this table):

I have deleted that part from the code


I've also added package-lock.json and yarn.lock to the IGNORE field, even though it's unrelated to this PR. If you want to, I can also create a separate PR for this.


The only thing, that's left (for now), would be tests.

(Also haven't looked into the Notes part of my PR description, but the most relevant part is generally loading marked extensions)

@nobkd nobkd marked this pull request as ready for review April 3, 2024 00:35
@nobkd
Copy link
Collaborator Author

nobkd commented Apr 7, 2024

Fixed the test, forgot some quotation marks... and for some reason the rendered md, is escaped, when run with nuekit.

Currently, the test is not run, because process.exit() which will be fixed with #254
But I tested it locally with the line commented out.

This is now ready for review.

@nobkd nobkd requested a review from tipiirai April 7, 2024 23:53
@@ -81,6 +81,9 @@ export async function createSite(args) {
self.is_empty = true
}

const markedConfig = joinRootPath(root, 'marked.config.js', true)
const { default: marked_extensions=[] } = await import(markedConfig).catch(() => ({}))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. Didn't even know about this default: .. syntax. Nice one!

Copy link
Collaborator Author

@nobkd nobkd Apr 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. Renames 'default' to 'marked_extensions' while unwrapping the variable from an object

@tipiirai
Copy link
Contributor

tipiirai commented Apr 8, 2024

Looks solid. I did some changes to how header id's are rendered on the dev branch. Can you check if this causes any conflicts when merging to dev?

Thanks!

@nobkd
Copy link
Collaborator Author

nobkd commented Apr 8, 2024

I'm relatively sure, it will result in merge conflicts.
I moved the variable renderer and marked.use so the Nue markdown renderer is loaded before other extensions.
But it shouldn't be that much of a problem, since I only moved it inside the file without changing it.

@nobkd

This comment was marked as outdated.

@nobkd

This comment was marked as outdated.

@nobkd

This comment was marked as outdated.

@nobkd
Copy link
Collaborator Author

nobkd commented Apr 18, 2024

When I use the workspace packages (using changes from #261), the tests with node+jest (on Linux) still fail, somehow connected to the dynamic import with absolute path not being found in the test, even though the file exists (in package checks checking for the file fail too though (jest)...)

Bun in contrast resolves the absolute path correctly without problems. The same goes for npm if I link the nuekit package to some test project. I think, it just fails in the jest environment...

I haven't checked it with bun+jest yet. If it still occurs there, I can be sure that jest is the culprit. It might also be an --experimental-vm-modules issue...
Not sure, what to do here and how to check where this issue comes from (other than being related to dynamic import())

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

Successfully merging this pull request may close these issues.

Allow adding Marked Extensions in NueJS
2 participants