Skip to main content

Flask extension to add docsify (documentation) sourced from discourse (forum) content.

Project description

discourse-docsify

Flask extension to source data from discourse forum posts into a docsify (documentation) website.

This allows you to leverage docsify's features while still benefiting from discourse's forum nature: beautiful documentation anyone within your community may expand on and contribute to.

This project was inspired by this canonicalwebeteam's repo.

Setup

Install with

pip install discourse-docsify

You can initialize the extension directly:

from flask import Flask
from discourse_docsify import Docs

app = Flask(__name__)
# for more config options and additional parameters see below
docs = Docs(app, config={
    'DOCS_HOMEPAGE_TITLE': 'My Documentation Website',
    'DOCS_DISCOURSE_API_USERNAME': '...',
    'DOCS_DISCOURSE_API_KEY': '...',
    'DOCS_DISCOURSE_BASE_URL': '...',
    'DOCS_DISCOURSE_INDEX_TOPIC_ID': '...',
})

or by doing

from flask import Flask
from discourse_docsify import Docs

# for more config options and additional parameters see below
docs = Docs(config={
    'DOCS_HOMEPAGE_TITLE': 'My Documentation Website',
    'DOCS_DISCOURSE_API_USERNAME': '...',
    'DOCS_DISCOURSE_API_KEY': '...',
    'DOCS_DISCOURSE_BASE_URL': '...',
    'DOCS_DISCOURSE_INDEX_TOPIC_ID': '...',
})

app = Flask(__name__)
docs.init_app(app)

No plugin needs to be installed on discourse, you simply need a valid API key.

Index topic

The index topic contains the homepage content, a list of urls mappings to translate documentation paths into forum topics and the documentation sidebar.

In this topic, # headings specify the start of a section. Each of the contents described above corresponds to a specific section. Here is the format the topic should have:

# Homepage
Welcome to my Documentation!
        
# URLs
[details=Mapping table]
| topic | path |
| -- | -- |
| forum-url/t/general-handbook/5258 | /general-handbook |
| forum-url/t/how-to-install/5256  | /installation |
| forum-url/t/developer-api-specs/5259 | /api |
[/details]

# Navigation
[details=Navigation]
| level | path | navlink |
| -- | -- | -- |
| 1 |  | Handbooks |
| 2 | general-handbook | General Handbook |
| 1 | installation | Installation Instructions |
| 1 | api | Dev API |

[/details]

The names of the sections are important (Homepage, URLs and Navigation).

In the URLs table, we have pairs of topic (forum link) and path (the documentation url path).

In the Navigation table, the level sets the indentation in the sidebar (meaning a level 2 item will be inside the level 1 item immediately above it), path is a path from the URLs table (or empty, if you want a linkless category) and navlink is the actual text in this sidebar navigation item.

Features and Description

Discourse-Docsify will create a blueprint at the /docs url prefix (can be changed through the config) which will serve the files requested by docsify by fetching the forum content for the topic associated with each path.

The original topic markdown is used (and not the discourse html generated output) so you may use any docsify compatible markdown.

A caching feature is also included to speed up loading times (as querying the forum for every request can be quite slow) which may either be through a simple python dictionary or through redis.

Optionally, one function to be run before content is served can also be passed to the Docs object.

The pre_content function

Docs accepts a third parameter: the pre_content function, a function run before each documentation request and that you may use to, for example, restrict access based on some sort of authentication:

def docs_auth_validation(path):
    if 'profile' not in session:
        return redirect('/login')
    if not session['profile']['admin']:
        return abort(403, 'Only admins may view this page.')

docs = Docs(app, {...}, docs_auth_validation)

This function has one argument: the current request's path.

Configuration reference

Setting name Default Description
DOCS_URL_PREFIX /docs The URL prefix for the blueprint that will be serving the docs.
DOCS_HOMEPAGE_TITLE Documentation Homepage title
DOCS_INDEX_PATH There is a default index.html file with a simple docsify example, which you can replace by giving the path to your own file here.
DOCS_TEMPLATE_PATH There is a default template to generate the documentation content (which appends edit buttons with a forum link and last updated time) but you can replace it with your own by entering its path here.
DOCS_HUMANIZE_LOCALE humanize is used to generate the "last updated" strings (2 hours ago, a month ago, etc). If you want to change the language used, enter a locale name here. Example: ru_RU for russian.
DOCS_CACHE_TIMEOUT 3600 * 10 Number of seconds for which to cache each page's content.
DOCS_CACHE_TYPE memory either memory (uses a simple python dictionary) or redis
DOCS_CACHE_REDIS_CONN {} if using redis, the redis connection arguments, passed to the Redis constructor.
DOCS_CACHE_REDIS_PREFIX discourse_docsify_docs_ Prefix to use for redis caching
DOCS_DISCOURSE_API_USERNAME Discourse API username. Needed to fetch forum content.
DOCS_DISCOURSE_API_KEY Discourse API key. Needed to fetch forum content.
DOCS_DISCOURSE_BASE_URL The discourse (forum) main url.
DOCS_DISCOURSE_INDEX_TOPIC_ID The ID of the index topic (the number at the end of the topic url).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

discourse-docsify-1.0.0.tar.gz (10.7 kB view details)

Uploaded Source

File details

Details for the file discourse-docsify-1.0.0.tar.gz.

File metadata

  • Download URL: discourse-docsify-1.0.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.5

File hashes

Hashes for discourse-docsify-1.0.0.tar.gz
Algorithm Hash digest
SHA256 faca86a923d6cd44a5d16ec22e1597869107f01b22713f9278663859531280f4
MD5 e7440c0858f4cb7821773d3623fdca27
BLAKE2b-256 86f53ee03bfb321dab787c91d1dd2a855e8770ffab50609b5dade6502a577cd1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page