My working environment is Linux/Ubuntu and Firefox. I have built a simple frontend editor which allows to open the corresponding md-file in the editor directly from the local HUGO website (http://localhost:1313/):
If necessary you have to set in Firefox:
Settings / Files and Applications -> âsimple text documentâ: Gedit (your prefered editor)
After that an âeditâ link is shown (only locally), which opens the corresponding md-file directly in the editor. For other operating systems and working environments this has to be adjusted accordingly.
{{ if and .Site.IsServer .Site.Params.projectPath }}<div style="float:right;"><a href="file://{{ .Site.Params.projectPath }}/content/{{ .File.Path }}">edit</a></div>{{ end }}
to avoid showing the edit link by calling hugo server without environment param.
This menu contains utilities which are helpful during development. E.g. a way to toggle a âLayoutâ view (useful for debugging CSS issues in browsers):
The âEdit Markdownâ option opens the Markdown file corresponding to the currently viewed page in the standard editor, e.g. MacVim or Notepad++. This works by having the link actually send a little HTTP GET request to localhost:1234/open/ and the Python script used to start the Hugo server handles that request and maps the given HTML page to the matching Markdown file â and opens it. Saving the editor makes Hugo (and thus the browser) reload automatically.
In the Hugo templates, itâs a matter of having
{{ if .Site.IsServer }}
{{ partial "devmenu" . }}
{{ end }}
in the baseof.html file. baseof.html then references the right CSS and includes any JavaScript and HTML to implement the menu. I.e. the menu is nicely separated from the real website.
The little Python server means that this approach works with all browsers, on all operating systems - no plugins are required. This reduces the pain for people to edit the web page.
I am particularly interested in simple solutions that are easy to maintain and easy to understand. My target audience is people who want to build and maintain small websites, without Github, Netlify etc.
The code isnât published anywhere, no. Thereâs not much to it though:
Instead of hugo server, we use this little Python script (called serve) to launch the Webserver: https://gist.github.com/frerich/b6436ec41e29307affc9c39142d21698 the script expects to be stored one level above the Hugo site directory; change the subprocess.Popen call in line 60 to suit your needs. After launching hugo server, it launches a small HTTP server on port 1314 which expects POST requests to /open?path=<filename>. This will open the given file in an editor (see the open_file function and adjust as needed). Whatâs important is that this Python script exports the path to the Hugo site directory via the HUGO_DIR environment variable.
In Hugo, a devmenu.html partial which I mentioned in my previous comment uses this JavaScript function to issue an XMLHttpRequest to the web server:
{{ with .File }}
function openSourceFile()
{
{{ $srcFilePath := printf "%s/content/%s" (getenv "HUGO_DIR") .Path }}
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://localhost:1314/open?path={{ $srcFilePath }}", true );
xhr.send(null);
}
{{ end }}