# What do you think? add a callback to Documents.Page() to allow modification of \*.md pages on the fly before parsing

**URL:** https://discourse.julialang.org/t/what-do-you-think-add-a-callback-to-documents-page-to-allow-modification-of-md-pages-on-the-fly-before-parsing/79217
**Category:** General Usage
**Tags:** documenter
**Created:** [April 8, 2022, 9:34am UTC](https://discourse.julialang.org/t/what-do-you-think-add-a-callback-to-documents-page-to-allow-modification-of-md-pages-on-the-fly-before-parsing/79217 "2022-04-08T09:34:27Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [April 8, 2022, 9:34am UTC](https://discourse.julialang.org/t/what-do-you-think-add-a-callback-to-documents-page-to-allow-modification-of-md-pages-on-the-fly-before-parsing/79217/1 "2022-04-08T09:34:27Z")

</div>

Hello,  
for my project I need to add some common parts to the markdown files that will be processed by Documenter.jl to become HTML pages, like a footer section to add a `@raw html` block to allow comments, a sort of header to add a list of videos, etc.  
I don’t want to do this by editing each md pages for several reasons.  
Do you think it would be reasonable to add a callback function in `Documents.page()`, just after the actual reading of the page and before the Markdown parsing, so that users can modify the page on the fly?

For example changing:

```julia
mdpage = Markdown.parse(read(source, String))

```

with something like:

```julia
pagecontent = read(source, String)
pagecontent = editsource(pagecontent,source)
mdpage = Markdown.parse(pagecontent)

```

and the default `editsource()` callback defined as:

```julia
editsource = (pagecontent,source) -> return pagecontent

```

I can take care to start a pull request for it, but I did want first to ask if it looks like a good idea to add this kind of flexibility or there are better ways to achieve it. What do yo think?  
Also, would be better an in-place version `editsource!()` ?

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [April 8, 2022, 9:45am UTC](https://discourse.julialang.org/t/what-do-you-think-add-a-callback-to-documents-page-to-allow-modification-of-md-pages-on-the-fly-before-parsing/79217/2 "2022-04-08T09:45:17Z")

</div>

Why not just manipulate the file before calling `makedocs`?

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [April 8, 2022, 9:54am UTC](https://discourse.julialang.org/t/what-do-you-think-add-a-callback-to-documents-page-to-allow-modification-of-md-pages-on-the-fly-before-parsing/79217/3 "2022-04-08T09:54:36Z")

</div>

Because:

- I don’t want to distract users of the raw files from this “presentation” details (the raw files are actual \*.jl files processed by Literate to .md that the students are invited to play with)
- if I want to change something in the way I manage these additional blocks, I would have to do it for all the pages, while with this approach I can change it only once in the make.jl script
- because it is boring to manually repeat a large code chunk when it only change vids of youtube and I can do it programmatically 🙂

I would like to keep in the callback the path of the file, so I can always apply my custom logic and filter where to do the modifications, or how to do it (in my case for example I will use a dictionary of vids per page) .

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [April 8, 2022, 9:59am UTC](https://discourse.julialang.org/t/what-do-you-think-add-a-callback-to-documents-page-to-allow-modification-of-md-pages-on-the-fly-before-parsing/79217/4 "2022-04-08T09:59:15Z")

</div>

> [@sylvaticus](#):
>
> the raw files are actual \*.jl files processed by Literate to .md

In that case, Literate already support [Custom pre- and post-processing](https://fredrikekre.github.io/Literate.jl/v2/customprocessing/).

> [@sylvaticus](#):
>
> if I want to change something in the way I manage these additional blocks, I would have to do it for all the pages

> [@sylvaticus](#):
>
> because it is boring to manually repeat a large code chunk

I mean’t that you would write a function that does it so you you would only have to change this function that you run on all the pages.

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [April 8, 2022, 10:06am UTC](https://discourse.julialang.org/t/what-do-you-think-add-a-callback-to-documents-page-to-allow-modification-of-md-pages-on-the-fly-before-parsing/79217/5 "2022-04-08T10:06:18Z")

</div>

Thank you. I wasn’t aware of it, but I do actually have a mix of .jl and original .md files in my project [[source](https://github.com/sylvaticus/SPMLJ/tree/main/lessonsSources) - [rendered](https://sylvaticus.github.io/SPMLJ/dev/) ], so having it in the final step (in Documenter) is more flexible…

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [April 10, 2022, 10:34am UTC](https://discourse.julialang.org/t/what-do-you-think-add-a-callback-to-documents-page-to-allow-modification-of-md-pages-on-the-fly-before-parsing/79217/6 "2022-04-10T10:34:28Z")

</div>

So, I had a look on the documentation on [Custom pre- and post-processing](https://fredrikekre.github.io/Literate.jl/v2/customprocessing/) functionalities on Literate, and indeed it’s exacly my need, just moved up to Documenter as I need it for oiriginal md and Literate processed jl files.  
So, should I go on and try to implement a `preprocess` callback for Documenter?

Would it make sense to pass to the callback the actual file content and the path?

---

<div class="post-metadata">

### Author: ![mortenpi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mortenpi/32/158_2.png) [@mortenpi](https://discourse.julialang.org/u/mortenpi)
#### Post date: [April 10, 2022, 9:58pm UTC](https://discourse.julialang.org/t/what-do-you-think-add-a-callback-to-documents-page-to-allow-modification-of-md-pages-on-the-fly-before-parsing/79217/7 "2022-04-10T21:58:07Z")

</div>

Do you want the preprocessing to happen at the text level or AST level? If the former, it still seems like something you could do yourself in `make.jl` between the Literate and `makedocs` calls?

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [April 13, 2022, 8:21am UTC](https://discourse.julialang.org/t/what-do-you-think-add-a-callback-to-documents-page-to-allow-modification-of-md-pages-on-the-fly-before-parsing/79217/8 "2022-04-13T08:21:43Z")

</div>

I could surely change the text file in make.jl before calling `makedocs`, but that would change the file on disk. And I don’t want that. First, because I build the documentation several times, at each new version of my project, secondly because I want to keep a distinction between the content of the page and the presentation/widgets/addons/ stuff that I want to add to my pages.  
And I don’t have only literate → md workflow (in that case I could use `postprocess` from Literate.jl), but I also have some “original” \*.md pages, so having the `preprocess` function in Documenter is the more natural option for my user case…

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [April 13, 2022, 9:19am UTC](https://discourse.julialang.org/t/what-do-you-think-add-a-callback-to-documents-page-to-allow-modification-of-md-pages-on-the-fly-before-parsing/79217/9 "2022-04-13T09:19:51Z")

</div>

ok, so I managed to add the `preprocess::Function` argument to `Documents.Document()` and save it as a new field on the `User` struct.

On the other hand, I added `preprocess` to `addPage!(doc, src, dst, workdir, preprocess)` that then call ` Page(src, dst, workdir, preprocess)`.

Now my point is how do I move the `preprocess` argument from the `User` struct to the `addPage!()` call ?

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [April 13, 2022, 9:32am UTC](https://discourse.julialang.org/t/what-do-you-think-add-a-callback-to-documents-page-to-allow-modification-of-md-pages-on-the-fly-before-parsing/79217/10 "2022-04-13T09:32:28Z")

</div>

ugh, I think I got it… in `Selectors.runner`… need to test it now…

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [April 13, 2022, 9:53am UTC](https://discourse.julialang.org/t/what-do-you-think-add-a-callback-to-documents-page-to-allow-modification-of-md-pages-on-the-fly-before-parsing/79217/11 "2022-04-13T09:53:22Z")

</div>

Works 🙂 🙂 🙂

I have created [a pull request](https://github.com/JuliaDocs/Documenter.jl/pull/1798)…
