How to offer the source code of the file that created the page

How could I create a template that offered in the bottom a link to the markdown/asciidoctor/whatever source code of the post the user is reading?

If you look at https://gohugo.io/variables/git/#readout there is a button at the bottom that links to https://github.com/gohugoio/hugoDocs/edit/master/content/en/variables/git.md and says, “Improve this page”. That’s one way.

You figure out how you are sharing the source document (in most cases that means linking to a network resource, such as a public URL), and then generate a link in your template based on git variable or a pattern that matches the file structure in the source. :slight_smile:

The problem is I would like to keep such content in my own page, and without having the file repeated.

For example, if you are viewing /posts/post1, then inside there would be a link to /posts/post1.adoc or something like that. That file would be the original file. The problem is hugo will use any file ended in adoc to generate an html. And then again, I don’t want to have a separate file which is a copy of the first one but with a different extension.

I don’t understand, your words appear to be contradictory.

How about this: what problem are you trying to solve? Hugo uses templates on source files to produce websites. It is common practice to link to where the source files are kept in version control, so others can access and do what they will with it. What problem are you encountering?

Let’s say I have a markdown file. I want that file to generate two pages. A raw page (non-html, just the contents of that file) and a processed page (what hugo typically does using a template and that file).

I think I could copy that file and change the extension to make hugo consider it as a resource, but I don’t want to duplicate all my files.

Maybe something like that could be achieved by using build options or output formats?

I don’t think I understood your question either. I always see ppl simply linking to the source file located in a repository, since it’s what Hugo is processing.

2 Likes

Actually, Hugo doesn’t need to work inside a repository, as far as I know. And yes, that is an option, but I would prefer to do it this way.

I know, but I frankly never saw anyone doing it any other way…

Anyway, I’m almost sure you could use output formats.

1 Like

I’m looking at that at the moment :slight_smile:. I will post here if I manage to make it work that way.

EDIT:
I got it working :slight_smile:. I added these lines to my config.toml file:

[mediaTypes]
  [mediaTypes."text/asciidoc"]
    suffixes = ["adoc"]

[outputFormats]
  [outputFormats.ASCIIDOC]
    baseName = "raw"
    mediaType = "text/asciidoc"
    isPlainText = "true"
    isHTML = "false"
    noUgly = "true"
    permalinkable = "true"

[outputs]
  page = ["HTML", "ASCIIDOC"]

and created a standar template by the name layouts/_default/single.adoc:

{{ .RawContent }}

Now, a new file called raw.adoc is created for each page, and it’s a matter of linking to it.

Thanks for redirecting me to the correct page of the documentation.

1 Like

Nice! Don’t forget to mark the topic as solved.

1 Like

I’m glad you figured this out.

Emphasis mine. The issue I take with your description is that you imply duplicating your file is not desirable, but you’ve added processing overhead by requiring each page be created via Hugo.

I get it, it’s quite cool. But it’s more resources than duplicating your files, so your description indicates a different solution that “duplication via templating”. :slight_smile:

Well, the “overhead” is making hugo process the file with a template that just puts the whole contents. I don’t think the overhead is more than 50ms even across a big website. Duplicating the file is always problematic, because you need to remember to update both if you intend to change them. Or for example, you may forget to create the copy for a new post and you end up with a broken link. That is a huge drawback.
In addittion, that does not change the fact that hugo would have interpreted a .adoc file as if to generate an html.

How is it more resources? In your final static page, you need two different files, no matter how you generate the raw file.
The only improvement could be for hugo to understand that it only needs to copy that file, and not process a template which is in fact like copying it. However, as stated, I haven’t noticed an impact in the build time.

I think I misunderstand the conversation, as you’ve answered your question from my perspective.

It works for you, good job. :slight_smile:

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.