Skip to content

Commit

Permalink
Do not fail if javascript lib is online and no Internet (#101)
Browse files Browse the repository at this point in the history
  - Fixed behavior of the `url_exists()` function
  - Added log levels (Warning)
  - Documented the change
  • Loading branch information
Laurent Franceschetti committed Sep 5, 2024
1 parent 502a3e8 commit 2fb1038
Show file tree
Hide file tree
Showing 7 changed files with 1,784 additions and 5 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.1.2, 2024-09-05

Changed: If the `javascript` parameter starts with http(s) and no Internet
access is available, a WARNING is now issued
(mkdocs no longer fails with an exception).

## 1.1.1, 2023-09-26

Fixed: Bug with local javascript library

## 1.1.0, 2023-09-01

Added: Parameter `javascript` in config file for optionally specifying the
Expand Down
47 changes: 42 additions & 5 deletions mermaid2/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,43 @@
log.addFilter(warning_filter)

MERMAID_LABEL = "MERMAID2 -" # plugin's signature label
def info(*args) -> str:
"Write information on the console, preceded by the signature label"


TRACE_LEVELS = {
'debug' : logging.DEBUG,
'info' : logging.INFO,
'warning' : logging.WARNING,
'error' : logging.ERROR,
'critical': logging.CRITICAL
}

def trace(*args, level:str='info'):
"""
General purpose print function, as trace,
for the mkdocs-macros framework;
it will appear unless --quiet option is activated.
The level is 'debug', 'info', 'warning', 'error' or 'critical'.
"""
args = [MERMAID_LABEL] + [str(arg) for arg in args]
msg = ' '.join(args)
log.info(msg)
try:
log.log(TRACE_LEVELS[level], msg)
except KeyError:
raise ValueError("Unknown level '%s' %s" % (level,
tuple(TRACE_LEVELS.keys())
)
)
return msg



def info(*args) -> str:
"Write information on the console, preceded by the signature label"
# args = [MERMAID_LABEL] + [str(arg) for arg in args]
# msg = ' '.join(args)
# log.info(msg)
return trace(*args)

# -------------------
# Paths and URLs
Expand All @@ -45,8 +77,13 @@ def libname(lib:str) -> str:
def url_exists(url:str, local_base_dir:str='') -> bool:
"Checks that a url exists"
if url.startswith('http'):
request = requests.get(url)
return request.status_code == 200
try:
request = requests.get(url)
return request.status_code == 200
except requests.exceptions.RequestException as e:
trace("Cannot check URL, no Internet access? (%s):\n\n%s" % (url, e),
level='warning')
return True
else:
pathname = os.path.join(local_base_dir, url)
return os.path.exists(pathname)
39 changes: 39 additions & 0 deletions test/http_lib/docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Mermaid test (simple)

## Mermaid usual
This is a test of Mermaid:

```mermaid
graph TD
hello --> world
world --> world2
```

> If you don't see a graph here, it's broken.
## Git Graph
This is a test of Git Graph:

```mermaid
gitGraph
commit
commit
branch develop
checkout develop
commit
commit
checkout main
merge develop
commit
commit
```


## Normal fences
This is usual fenced code (with no highlighting)

```python
for page in pages:
page.read()
```

1,642 changes: 1,642 additions & 0 deletions test/http_lib/docs/js/mermaid.min.js

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions test/http_lib/docs/second.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Second page
Testing special cases

## Wrong diagram

```mermaid
graph FG
A[Client]
```

## Correct

```mermaid
graph TD
A[Client] --> B[Load Balancer]
```

## Other

```mermaid
graph TD
A[Client] --> B[Load Balancer]
B --> C[Server01]
B --> D[Server02]
```
17 changes: 17 additions & 0 deletions test/http_lib/mkdocs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
site_name: Mermaid test (simple, with local specified library)
site_description: Test for mermaid
docs_dir: docs # indispensable or readthedocs will fail
theme: readthedocs # you may want to try windmill?


nav:
- Main: index.md
- Second: second.md

plugins:
- search
- mermaid2:
javascript: https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs



9 changes: 9 additions & 0 deletions webdoc/docs/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ changing the version number to determine the version you want
https://cdn.jsdelivr.net/npm/mermaid@10.2.0/dist/mermaid.min.js
```


!!! Note
No explicit call to `mermaid.initialize()` is required, since it is
automatically inserted by the plugin.
Expand All @@ -80,6 +81,14 @@ https://cdn.jsdelivr.net/npm/mermaid@10.2.0/dist/mermaid.min.js
on its ownlwill definitely **not** work, since there will be broken
links.

!!! Warning "Behavior in case of incorrect URL/no Internet access"
1. An incorrect URL will cause an error that aborts MkDocs.
2. If the address starts with http(s) and no Internet access
is available at time of compile, MkDocs-Mermaid will continue and issue
a WARNING. That behavior is for containers that do not
have necessarily have Internet access at compile time
(however, if you want to abort
in that case use the strict mode: `mkdocs build --strict`.

## Using `extra_javascript`

Expand Down

0 comments on commit 2fb1038

Please sign in to comment.