Composing Functions with compose3:31 with Craig Dennis
Compose allows you to prepend your function to the start of the function chain.
Whoops! I forgot to use the Markdown input I made. HTML can be passed through CommonMark without a problem. So here is the updated code, also available in the repository.
String result = Renderer.render(htmlInput, markeddownSanitizer); System.out.println("HTML is safe..."); System.out.println(result); result = Renderer.render(markdownInput, markeddownSanitizer); System.out.println("Markdown is too..."); System.out.println(result);
Okay, so 0:00 now our application wants to allow users to submit Markdown instead of HTML. 0:01 And we already have Markdown in our library because we are using an open 0:06 source library called CommonMark. 0:10 And it's made by the people over at Atlassian. 0:12 And I just wanted to give a quick shout out here and thank companies who do that, 0:15 who allow us to use things. 0:20 They've built these things, it's very common, you just wanna render a document, 0:21 you pass it some text and it turns into HTML. 0:25 And let's take a look at what some of that Markdown looks like. 0:27 But big shout out to those of you out there that are making and 0:30 sharing open source work. 0:34 It really makes a difference. 0:36 So let's make a Markdown input here. 0:37 And I wanna make it the same as what's here. 0:41 So, it's something like this. 0:43 Using strong tags, and then to make a strong tag, 0:46 you do ** allows you to over promote. 0:51 All right, we don't want that on our composure pages. 0:55 And links look like this. 0:58 So, Links open in a new browser. 1:00 So that's the text inside of it. 1:05 I'm running out of space over here. 1:09 Making use of this nice feature, just press Enter, look at that. 1:12 Told you, it thinks of everything, speaking of open source. 1:16 Okay, and we're gonna say http://www.google.com. 1:19 Great, so now we have some Markdown input, and 1:25 that method over there that we saw is Markdown and it's called render. 1:29 And it takes a string, and it returns a string. 1:34 And that string is HTML. 1:38 So right now we have our sanitizer, we're moving forward but 1:39 I'm just gonna run this Markdown input through so you can see what happens. 1:42 Our strong tag is back and our link doesn't have external on there. 1:48 No, we've got to sanitize our stuff again. 1:55 But we already have the sanitizer, right? 1:57 We already have this right here. 1:59 So my guess is that we're just gonna compose these together, right? 2:01 But we have a problem, before we did this and then we put it at the end but 2:04 now we need to put it at the beginning. 2:08 So this is a good time to introduce the compose method on a function. 2:12 So if I do sanitizer and then I do .compose. 2:16 This basically just works the other way. 2:21 So what happens is it's gonna run this first and 2:23 pass that into this function which is then I got to pass this into this. 2:25 So it's like putting it at the front of the chain. 2:29 Let's take a look and see what happens. 2:32 And boom. 2:36 We've got rid of our strong tag and our rel external is visible. 2:36 And this is because it ran this method first. 2:41 It took the Markdown, passed it into the sanitizer which again, 2:43 the sanitizer is this function all the way through. 2:46 So, as you can imagine, I can also take this here and 2:49 I can extract a variable again and 2:54 I can call this markeddownSanitizer. 2:58 And of course I could export that and 3:02 share that wherever I want in the code base. 3:04 Pretty slick, right? 3:07 So this is a great example of breaking down composable function, so 3:09 that you can create a functional pipeline. 3:13 It's a great way to keep your composure as these feature requests are coming in. 3:15 And that brings us to the end of this workshop. 3:20 Please, let me know what you thought of it. 3:22 I'd love to hear your feedback and if you've got more ideas for 3:24 things that we can break down like this, let me know. 3:27 See you soon. 3:29
You need to sign up for Treehouse in order to download course files.Sign up