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

Feature Request: Replace with contents from a file #4

Closed
rasmus-kirk opened this issue May 15, 2023 · 9 comments
Closed

Feature Request: Replace with contents from a file #4

rasmus-kirk opened this issue May 15, 2023 · 9 comments
Labels
enhancement New feature or request

Comments

@rasmus-kirk
Copy link

rasmus-kirk commented May 15, 2023

I have a folder of examples in my project and I would like to use this plugin to replace references to these example files with the actual code, fx:
::--example: someExample.ts, 15, 32--::
Would become a code block containing lines 15-32 of the someExample.ts file.

The thing that is preventing me from accomplishing it with the current version of this plugin, is that it doesn't seem to support replacing snippets with the contents of other files.

Would you consider supporting this usecase with this plugin? Do you know other non-hacky ways of doing this?

@krisztianb krisztianb added the enhancement New feature or request label May 15, 2023
@krisztianb
Copy link
Owner

Hi. Yes, you are right that the plugin currently doesn't support this. It only supports replacing text (matched by a Regex) with another text.

As far as I know you can include markdown files in comments like this:

/**
 * My test class.
 *
 * [[include ./test.md]]
 */
class Test {}

Does that help you?

To be honest this seems like a rather big change to the plugin. Maybe one should write a new one specifically for this task.

@rasmus-kirk
Copy link
Author

All right, thanks for the response. I'll either fork/create a new plugin or just link to the examples using hyperlinks instead.

@krisztianb
Copy link
Owner

Another solution came to my mind: One could define a function instead of the text that should be used as the replacement. Then the function could do the work (reading things from a file etc.). But I'm not sure if TypeDoc can be configured using a .js instead of a .json file. I will have to investigate this further.

@krisztianb krisztianb reopened this May 16, 2023
@rasmus-kirk
Copy link
Author

rasmus-kirk commented May 16, 2023

That would be a lot more powerful and still pretty clean.

Maybe you could define it in a .json and provide a path to a ts/js file? Something like:

"replaceText": {
    ...
    "includeFunctions": "/some/path/to/functions.ts",
    "replacements": [
        {
            "pattern": "somePattern",
            "replace": "functionDefinedInIncludedFile"
        },
    ]
}

I still do think configuring in js is the better solution, but if its not possible

@krisztianb
Copy link
Owner

TypeDoc does allow JavaScript configuration files so this should be doable. I will fiddle around with it and let you know.

@krisztianb
Copy link
Owner

krisztianb commented May 19, 2023

I got this working and you can install it as 2.3.0-beta from NPM.

The replacer function is simply forwarded as an argument to the String.replace method. Here is the information on how that works: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_function_as_the_replacement

There is also a very basic example in the updated README of the beta package.

See how it works for you and please give me feedback.

@rasmus-kirk
Copy link
Author

Hello, I installed it and played around with it. It was actually able to do the things I wanted (inlining code examples and linking to specific git commits) and it worked pretty well. I did however later find a repo that provided me with what I needed in a neater way.

I will post my config in a codesnippet in this issue as an example of how the replacer function can be used, either today or tomorrow. After that I will go ahead and close the issue. Thank you.

@krisztianb
Copy link
Owner

Thanks for reporting back. Glad to hear that you found a solution.

@krisztianb
Copy link
Owner

Released in 3.0.0

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

No branches or pull requests

2 participants