Finding Markdown Parser0:00
Alright, welcome back. So in the last episode, we left off right here, a code cliffhanger of sorts. So we figured out how to read a markdown file, and we learned how to ultimately, once we grab that file, we are caching it with this key here. But yeah, we haven't yet figured out how are we fetching the file, how are we compiling it down, what does replace links mean? So the first step I see is markdown. So I assume this is using a dedicated package. So we can see, nope, it's not pulling in anything, it's a global. So let's do this, let's go into composer.json, and let's see if it's autoloading anything special. It's not. Hmm. So let's look for markdown. Okay, so it is using the League\CommonMark package called CommonMark. Let's look at that, packages.org/packages/league/commonmark. There we go. It's a markdown parser for PHP based on the CommonMark spec. So of course, in these situations, if you need to dig in further, we can take a look. You install it
There we go. It's a markdown parser for PHP based on the CommonMark spec. So of course, in these situations, if you need to dig in further, we can take a look. You install it here, basic usage. So the CommonMarkConverter class provides a simple wrapper for converting CommonMark to HTML. So you instantiate the class, and then you say, convert this markdown block to HTML, and then this is what you get on the other side. And also, if you will be parsing untrusted input from users. So in this case, I'd be curious, maybe the Laravel.com repo doesn't have to worry about that, because everything you're parsing is your own data, and you can trust your own markdown. But otherwise, I guess you need to set these options there. Anyways, now there is, it does seem like there's a helper called markdown. So I'm going to look for this. Okay, in bootstrap helpers. Okay, so bootstrap, there's a file here called helpers.php. And there it
Inspecting markdown Helper1:45
it does seem like there's a helper called markdown. So I'm going to look for this. Okay, in bootstrap helpers. Okay, so bootstrap, there's a file here called helpers. And there it is. Oh, but it looks like it's using parsedown-extra. Are these connected? I thought those were two different. I'm not too familiar with either of them. But I thought these were two different. Hmm, let's look at this together. parsedown-extra. Let's take a look at that. Markdown extra extension for parsedown. So you knew that up. Okay. And then you call text. So it seems like these do the same thing. Are they connected? Is this just an old package that maybe maybe the Laravel docs originally use this and now they use this one? I'm not sure. Maybe commonmark is required. No, I don't know. I'll look into that later. But maybe that's just an old dependency. I'm not sure. Anyways, so that news up parsedown-extra, and then it compiles
Compiling Markdown to HTML2:38
common mark is required. No, I don't know. I'll look into that later. But maybe that's just an old dependency. I'm not sure. Anyways, so that news up parse down extra, and then it compiles the text. And you'll remember right here, it's passing this files.getPath. So I assume that's the file instance. Let's look at the file system. So that's the equivalent as when with the Facade, you say file::get, and then you give it a path. So we're getting the contents of resources/docs, and then any of these files like installation. Okay. So if you want to take a look at that real quick together. Alright, let's come back to Chrome, give that a refresh. And sure enough, you can see it is just getting the contents of that as a string. Okay, so we're compiling that down. So let's do it again. But this time, run it through that markdown helper. Give it a refresh. And now you can see it has been turned into HTML. So once again, here's what it was before
How Helpers Are Loaded3:28
down. So let's do it again. But this time, run it through that markdown helper. Give it a refresh. And now you can see it has been turned into HTML. So once again, here's what it was before markdown. And then after it's all been compiled down. So yeah, that's an example of you don't need to write all the code to compile markdown yourself. It's already been done by plenty of packages. So just pull in one of those and then pass your data to it. Now if you're curious though, this helpers file, this is a common thing. I do this myself as well. Just basic helper functions that you'll frequently use across your project. You don't want to go crazy with them, but a small smattering of them can actually be incredibly useful. And this is a good example of that. So if you're curious about how this gets loaded, it can be in a number of ways. So sometimes it'll be loaded from the composer.json file. I don't see any helpers here, but you could. So in your
Replacing Version Links5:06
so I guess all of the links. Okay, yeah, here it is. So for all the links, it looks like it's using this kind of template syntax. So let's do this one more time. What do you get when you refresh? So is this still part of it? Yeah, so all of the links are going to the correct page, but it's using this little placeholder variable for version. So I assume replaceLinks is just going to look for that and then replace it with the current version you're using. And yep, do a simple str_replace, look for every instance of that, replace it with 5.6 using this big content here as a base. Okay, so one more time. Let's do, actually, let's do this. Let's just dd. And there you go. So it used to be version, but now it's just swapping that out. So if I did, well, 5.5 won't work, but I'm going to show you this, actually. Let's do resources/docs/5.5/foobar.md. And now let's grab the 5.6 docs,
Tracing Docs Route Flow5:53
version, but now it's just swapping that out. So if I did, well, 5.5 won't work, but I'm going to show you this, actually. Let's do resources/docs/5.5/foobar.md. And now let's grab the 5.6 docs, like so. Okay, so now if I give that a refresh, you will see the version is properly updated to 5.5. So now that link will be to the correct URL for the 5.5 version of the, in this case, migrations page. But if you're on 5.6, then of course you want to see the 5.6 version of that documentation. Okay, so we're starting to make good progress. We now fully understand what every step of this does. So if we come back to our routes page, we can see when you visit a specific doc, it will go to DocsController show. We've reviewed that, we set the default section page, and then we say docs, get me the compiled content. So if I were to return that and give this a refresh, we now have the compiled content from the markdown file. And remember, for
