What the process looks like? It’s simple:
Ok. So how does it work?
We trigger pre and postbuild hooks that update NextJS project with Obsidian-written docs.
Depending on the goal and size of your docs, you can have 2 choices where to settle the Obsidian vault.
I personally chose the first option and this option will be discussed here.
You can start creating docs in an exisitng repo or prepare beforehand. In the real world it’s more probably that the docs are result of something that you have already made so feel free to crete docs folder in your repo root (also consider learning about documentation driven development).
Obsidian vault is a name for opening local folder and using it in Obsidian program. The vault name is there to remind you that Obsidian can have a sligltly different formatting or needs (e.g. using tags). Remember that if you had some docs created already, there might be problem with downloading your vault to Obsidian - if that happens make sure about custom formatting that comes with obsidian. You can adjust it in the program’s settings. You can always use obsidian plugins that help with formatting (if yours was not quite markdown/obsidian aligned)
You can treat this whole process as an Obsidian md files publish alternative. The main way would be to use Obsidian paid tier. This does not fit into our goal where the docs are located in a specific URL and can be build and queried by Google robots entering the site we are documenting. This is an alternative that uses official Obsidian way of creating docs - not completly other software.
In your package.json file has scripts. Usually you run npm run dev or npm run test, sometimes build. What is less common to know is that when you run npm run build npm will execute prebuild script before and after it.
We need one pre script and it looks like that:
// package.json file
//...
"scripts": {
"dev": "next dev",
"prebuild": "node ./src/scripts/UpdateAppTutorials.js",
"build": "next build",
//...
You can see that now we need a file caleed UpdateAppTutorials.js and it’s need to be located in the root frontend folder with our nextjs project. Continuing the example from the point 1 it will now look like this: /main_repo/frontend/src/scripts/UpdateAppTutorials.js.
That script will be responsible for copying the files from /main_repo/docs/ to /main_repo/frontend/src/pages/docs/
The script should contain some node js code that will do the job:
// UpdateAppTutorials.js
// All documentation is written in docs folder in root of the repo.
// Content of docs/tutorials is visible on the page after build
// This script runs on prebuild and updates changes made to docs so they are ready for build
const fse = require('fs-extra')
const path = require('path');
const DOCS_MAIN = path.join(process.cwd(), '../docs/tutorials')
const DOCS_NEXTJS = path.join(process.cwd(), '/src/pages/docs')
try {
fse.copySync(DOCS_MAIN, DOCS_NEXTJS, { overwrite: true })
console.log('Copied files from main docs repo folder!')
} catch (err) {
console.error(err)
}
Don’t forget to install fse & path.
Perfect.
What now? Now you need to have logic within your NextJS project that deals with markdown files and turns them into pages.
If you do not have that part and want to know, drop me a msg to let me know if there is a need for such a tutorial.