Skip to content

Commit

Permalink
feat: Markdown story replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
cfoust committed Nov 27, 2023
1 parent 231ad79 commit 54c91ed
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ jobs:
curl -sSL $url | tar -xz --directory=./mdbook
echo `pwd`/mdbook >> $GITHUB_PATH
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.19"

- name: Build book
run: |
go run cmd/docs/main.go api > docs/src/generated-api.md
Expand All @@ -35,10 +40,6 @@ jobs:
with:
repository: cfoust/cy

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: "1.19"
- name: Build
run: >
go build
Expand Down
3 changes: 3 additions & 0 deletions docs/book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ authors = ["Caleb Foust"]
[output.html]
default-theme = "ayu"
preferred-dark-theme = "ayu"

[preprocessor.storybook]
command = "python3 storybook.py"
55 changes: 55 additions & 0 deletions docs/storybook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import json
import sys
import re
import hashlib

STORY_REGEX = re.compile("{{((\w+).)?(png|gif) (.+)}}")

if __name__ == '__main__':
args = sys.argv
if len(args) > 1 and args[1] == "supports":
sys.exit(0)

context, book = json.load(sys.stdin)

# all the rendering jobs that need to be done
jobs = {}

for section in book['sections']:
if not 'Chapter' in section:
continue

content = section['Chapter']['content']
replace = []

for ref in STORY_REGEX.finditer(content):
type_ = ref.group(3)
filename = ref.group(2)
command = ref.group(4)
if len(command) == 0:
continue

# The filename is the hash of the args, or can be specified in
# Markdown
if not filename:
h = hashlib.new('sha256')
h.update(command.encode('utf-8'))
filename = h.hexdigest()[:12]

filename += "." + type_

jobs[filename] = command
replace.append(
(
ref.start(0),
ref.end(0),
f"![{command}](./stories/{filename})",
)
)

for start, end, text in reversed(replace):
content = content[:start] + text + content[end:]

section['Chapter']['content'] = content

print(json.dumps(book))

0 comments on commit 54c91ed

Please sign in to comment.