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

Find and replace strings in HTML files #77

Open
samny opened this issue Jul 17, 2017 · 3 comments
Open

Find and replace strings in HTML files #77

samny opened this issue Jul 17, 2017 · 3 comments

Comments

@samny
Copy link

samny commented Jul 17, 2017

Hi, have the need to be able to replace some contents in the resulting HTML after they're generated. I tried "string-replace-webpack-plugin" and "html-replace-webpack-plugin" but could not get them to work. Any ideas how to accomplish this?

@iansinnott
Copy link
Owner

Well, off-hand my first thought is to just do it on the CLI in a post build hook. I.e. within package.json you can add postbuild to the scripts section and it will run right after every build. In that command you can use use sed (or any other CLI replacement tool) to replace text within the files and write them back to the same place. For example:

cat ./build/index.html | sed 's/Matched/Replaced/' > ./build/index.html

Of course this is just using unix to your advantage rather than doing anything with javascript.

What exactly is the use case here? Why not modify your template file and just generate the HTML you will want in the end?

@samny
Copy link
Author

samny commented Jul 18, 2017

Thanks @iansinnott, I will try out your suggestion.

Sorry, I should have explained my use-case. I'm working on a site that has most pages rendered by Django but also some React SPA pages. To be able to use some common presentational components across, I would like to generate them as partials with template variables instead of content. The main problem I found is that React HTML encodes all quotes etc. and since I found no way of changing Reacts behaviour my idea was to replace all the encoded entities back to what they need to be (ie. replace " with "). Also in the case of partials I need to get rid of the html doctype.

@iansinnott
Copy link
Owner

Ah, interesting. I hadn't thought of that use case. Yeah you could still use sed for this, but you could also use dangerouslySetInnerHTML on react components in order to bypass its character escaping. I guess that would only work for inner html though, and not attributes...

If you have a CLI tool to unescape HTML you might be able to simply run the whole HTML file through that program.

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

No branches or pull requests

2 participants