Skip to content

Commit b8814db

Browse files
fix(blog): improve code snippet formatting and remove redundant sections in DataWeave article
1 parent e15c03c commit b8814db

1 file changed

Lines changed: 14 additions & 14 deletions

File tree

‎src/content/blog/dataweave-scripts-to-clean-your-xml-html-code-snippets-for-a-wordpress-blog-post.mdx‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ draft: false
88
heroImage: ../../assets/blog/dataweave-scripts-to-clean-your-xml-html-code-snippets-for-a-wordpress-blog-post-1.png
99
---
1010

11-
In this post:
12-
13-
- The problem
14-
- First approach: XML input
15-
- Second approach: plain text input
16-
1711
In case you're not familiar with my [dataweave-scripts](https://github.com/alexandramartinez/dataweave-scripts) GitHub repo, it's the place where I keep some of the scripts I've created to help the community with transformation questions or simply some scripts that have been handy to me.
1812

1913
In this post, I want to introduce you to two transformations I added because of a use case I came up with last week. Basically to help clean an XML or HTML to publish a script in a WordPress article.
@@ -24,11 +18,11 @@ This problem started because I had written a blog post in a WordPress-based blog
2418

2519
![Maven XML snippet in WordPress with its tags stripped out, leaving only the bare values](../../assets/blog/dataweave-scripts-to-clean-your-xml-html-code-snippets-for-a-wordpress-blog-post-2.png)
2620

27-
The fix was simple. Instead of having the regular < and > characters pasted in the code snippet, I had to use < and > respectively.
21+
The fix was simple. Instead of having the regular `<` and `>` characters pasted in the code snippet, I had to use `&lt;` and `&gt;` respectively.
2822

2923
*(Thanks so much Julian Duque for providing the fix! I had no idea about this issue in WordPress* 🤗*)*
3024

31-
For example, instead of writing &lt;plugin>, I had to replace it with &lt;plugin>
25+
For example, instead of writing `<plugin>`, I had to replace it with `&lt;plugin&gt;`
3226

3327
I thought to myself: If I need to keep doing this for future blog posts, maybe I can create a DataWeave transformation to fix this for me so I can just easily copy and paste the new clean snippet.
3428

@@ -44,26 +38,32 @@ output text/plain
4438
---
4539
write(payload,"application/xml")
4640
replace "<?xml version='1.0' encoding='UTF-8'?>\n" with ""
47-
replace "<" with "<"
48-
replace ">" with ">"
41+
replace "<" with "&lt;"
42+
replace ">" with "&gt;"
4943
```
5044

45+
> [!PLAYGROUND]
46+
> [Open in the Playground](https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo&repo=alexandramartinez%2Fdataweave-scripts&path=functions%2Fclean-xml)
47+
5148
However, I quickly ran into issues when I tried to clean an HTML code snippet using this same transformation. This is how I came up with the second approach.
5249

5350
## Second approach: plain text input
5451

55-
This time I decided to use a plain text input instead of an XML input format. This way, both XML and HTML code snippets could be used as the input and I wouldn't need to use the write() function in the first place.
52+
This time I decided to use a plain text input instead of an XML input format. This way, both XML and HTML code snippets could be used as the input and I wouldn't need to use the `write()` function in the first place.
5653

5754
```dataweave
5855
%dw 2.0
5956
output text/plain
6057
---
6158
payload
62-
replace "<" with "<"
63-
replace ">" with ">"
59+
replace "<" with "&lt;"
60+
replace ">" with "&gt;"
6461
```
6562

66-
Plus, I got rid of one replace() because I no longer needed to remove the XML header.
63+
> [!PLAYGROUND]
64+
> [Open in the Playground](https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo&repo=alexandramartinez%2Fdataweave-scripts&path=functions%2Fclean-html)
65+
66+
Plus, I got rid of one `replace()` because I no longer needed to remove the XML header.
6767

6868
It's a short post, but I hope it's insightful for you all 🤗 I'm sure I'll keep using this example in the Playground to modify my WordPress posts in the future.
6969

0 commit comments

Comments
 (0)