Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 18 additions & 87 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ jobs:
- name: Check formatting
run: nix build -L .#checks.x86_64-linux.mdbook-check-code-fmt

markdown-formatting:
name: Markdown Formatting
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v4

- name: Install Nix
uses: DeterminateSystems/determinate-nix-action@v3

- name: Setup Magic Nix Cache
uses: DeterminateSystems/magic-nix-cache-action@v8

- name: Check markdown formatting
run: nix build -L .#checks.x86_64-linux.markdown-format-check

integration-tests:
name: Integration Tests
runs-on: ubuntu-latest
Expand All @@ -81,90 +99,3 @@ jobs:

- name: Run integration tests
run: nix build -L .#checks.x86_64-linux.runE2ETests

#build:
# name: Build ${{ matrix.system }}
# needs: check
# runs-on: ${{ matrix.runner }}
# permissions:
# id-token: write
# contents: read
# strategy:
# fail-fast: false
# matrix:
# include:
# - system: x86_64-linux
# runner: ubuntu-latest
# - system: aarch64-darwin
# runner: macos-14
# steps:
# - uses: actions/checkout@v4

# - name: Install Nix
# uses: DeterminateSystems/determinate-nix-action@v3

# - name: Setup Magic Nix Cache
# uses: DeterminateSystems/magic-nix-cache-action@v8

# - name: Build mdbook-check-code
# run: nix build .#mdbook-check-code

# - name: Create tarball
# run: |
# mkdir -p dist
# cp result/bin/mdbook-check-code dist/
# tar -czf mdbook-check-code-${{ matrix.system }}.tar.gz -C dist mdbook-check-code

# - name: Upload artifact
# uses: actions/upload-artifact@v4
# with:
# name: mdbook-check-code-${{ matrix.system }}
# path: mdbook-check-code-${{ matrix.system }}.tar.gz
# if-no-files-found: error

# release:
# name: Create Release
# needs: build
# runs-on: ubuntu-latest
# if: github.ref == 'refs/heads/main'
# permissions:
# contents: write
# steps:
# - uses: actions/checkout@v4

# - name: Download all artifacts
# uses: actions/download-artifact@v4
# with:
# path: artifacts

# - name: Prepare release assets
# run: |
# mkdir -p release
# find artifacts -name "*.tar.gz" -exec cp {} release/ \;
# ls -lah release/

# - name: Generate release tag
# id: tag
# run: |
# SHORT_SHA=$(git rev-parse --short HEAD)
# TAG="v0.1.0-${SHORT_SHA}"
# echo "tag=${TAG}" >> $GITHUB_OUTPUT

# - name: Create Release
# uses: ncipollo/release-action@v1
# with:
# artifacts: "release/*.tar.gz"
# tag: ${{ steps.tag.outputs.tag }}
# name: "Release ${{ steps.tag.outputs.tag }}"
# body: |
# ## Automated Release

# Built from commit ${{ github.sha }}

# ### Platforms
# - x86_64-linux
# - aarch64-darwin (Apple Silicon)

# Generated automatically by GitHub Actions
# draft: false
# prerelease: false
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ The preprocessor validates all code blocks during the build process and reports
## Configuration

All language behavior is configured in `book.toml`. Each language requires:

- `enabled` (bool) - Whether to check this language
- `compiler` (string) - Compiler executable (supports `${VAR}` env var expansion)
- `flags` (array) - Compiler flags

Optional:

- `preamble` (string) - Code prepended to all blocks
- `fence_markers` (array) - Custom fence identifiers

Expand Down
42 changes: 41 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@

fixture-src = gitignoreSource ./tests/fixtures;

# Map script names to their specific dependencies
scriptDeps = {
format-markdown = with pkgs; [ git nodePackages.prettier ];
# Future scripts and their dependencies go here
};

# Read all scripts from scripts/ directory
scriptFiles = builtins.readDir ./scripts;

# Wrap each script with its dependencies
wrappedScripts = pkgs.lib.mapAttrs' (filename: _:
let
# Use filename as script name (removes .sh extension if present)
scriptName = pkgs.lib.removeSuffix ".sh" filename;
# Get script-specific dependencies (empty list if not defined)
deps = scriptDeps.${scriptName} or [];
in
pkgs.lib.nameValuePair scriptName (
pkgs.writeShellApplication {
name = scriptName;
runtimeInputs = deps;
text = builtins.readFile (./scripts + "/${filename}");
}
)
) scriptFiles;

in {
packages = {
inherit mdbook-check-code;
Expand All @@ -67,6 +93,17 @@
# Check formatting
mdbook-check-code-fmt = craneLib.cargoFmt { inherit src; };

# Check Markdown formatting
markdown-format-check = pkgs.runCommand "markdown-format-check" {
buildInputs = [ wrappedScripts.format-markdown ];
src = gitignoreSource ./.;
} ''
cd $src
format-markdown --check
mkdir -p $out
echo "Markdown formatting check passed" > $out/result
'';

# The script inlined for brevity, consider extracting it
# so that it becomes independent of nix
runE2ETests = pkgs.runCommand "e2e-tests" {
Expand Down Expand Up @@ -125,7 +162,7 @@
nodejs
nodePackages.typescript
solc
];
] ++ (builtins.attrValues wrappedScripts);

shellHook = ''
export CLANG="${sunscreen-llvm-pkg}/bin/clang"
Expand All @@ -139,6 +176,9 @@
echo " tsc - TypeScript compiler"
echo " solc - Solidity compiler"
echo ""
echo "Helper scripts: ${builtins.concatStringsSep ", " (builtins.attrNames wrappedScripts)}"
echo " (run with -h or --help for usage)"
echo ""
echo "Environment variables:"
echo " CLANG=${sunscreen-llvm-pkg}/bin/clang"
'';
Expand Down
84 changes: 84 additions & 0 deletions scripts/format-markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env bash
set -euo pipefail

# Format markdown files in the repository using Prettier
# This matches the formatting that Zed editor uses

show_help() {
cat <<EOF
Format markdown files using Prettier

Usage:
format-markdown Format all markdown files (git-tracked, or all if not in repo)
format-markdown --check Check formatting without modifying files
format-markdown <file> Format specific file
format-markdown -h Show this help message

Options:
-h, --help Show this help message
--check Check formatting (exit 1 if files need formatting)
EOF
}

# Find all markdown files using git if available, otherwise use find
find_markdown_files() {
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
# In git repo: use git ls-files
git ls-files "*.md"
else
# Not in git repo (e.g., nix sandbox): use find
find . -name "*.md" -type f
fi
}

# Process arguments
CHECK_MODE=false
TARGET_FILE=""

if [ $# -eq 0 ]; then
# No arguments: format all markdown files
CHECK_MODE=false
elif [ "$1" = "--check" ]; then
# Check mode
CHECK_MODE=true
elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
show_help
exit 0
else
# Format specific file
if [ ! -f "$1" ]; then
echo "Error: file not found: $1" >&2
exit 1
fi
TARGET_FILE="$1"
fi

# Execute formatting or checking
if [ -n "$TARGET_FILE" ]; then
# Format specific file
prettier --write "$TARGET_FILE"
echo "Formatted $TARGET_FILE"
elif [ "$CHECK_MODE" = true ]; then
# Check all markdown files
files=$(find_markdown_files)
if [ -z "$files" ]; then
echo "No markdown files found"
exit 0
fi

if ! echo "$files" | xargs prettier --check; then
echo "Error: markdown files need formatting. Run 'format-markdown' to fix." >&2
exit 1
fi
echo "Markdown formatting check passed"
else
# Format all markdown files
files=$(find_markdown_files)
if [ -z "$files" ]; then
echo "No markdown files found"
exit 0
fi

echo "$files" | xargs prettier --write
echo "Markdown formatting complete"
fi
4 changes: 2 additions & 2 deletions tests/fixtures/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
- [TypeScript Examples](other_langs/ts_examples/README.md)
- [TypeScript Example](other_langs/ts_examples/typescript_example.md)
- [Solidity Example](solidity_example.md)
<!-- Uncomment to test error output: -->
<!-- - [Test Error](test_error.md) -->
<!-- Uncomment to test error output: -->
<!-- - [Test Error](test_error.md) -->
10 changes: 5 additions & 5 deletions tests/fixtures/src/other_langs/ts_examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ TypeScript type checking works seamlessly with the preprocessor:

```typescript
interface Config {
host: string;
port: number;
secure?: boolean;
host: string;
port: number;
secure?: boolean;
}

function getUrl(config: Config): string {
const protocol = config.secure ? 'https' : 'http';
return `${protocol}://${config.host}:${config.port}`;
const protocol = config.secure ? "https" : "http";
return `${protocol}://${config.host}:${config.port}`;
}
```
10 changes: 5 additions & 5 deletions tests/fixtures/src/other_langs/ts_examples/typescript_example.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@ Here's a simple TypeScript function:

```typescript
function add(a: number, b: number): number {
return a + b;
return a + b;
}
```

## Using interfaces

```typescript
interface User {
name: string;
age: number;
name: string;
age: number;
}

function greet(user: User): string {
return `Hello, ${user.name}! You are ${user.age} years old.`;
return `Hello, ${user.name}! You are ${user.age} years old.`;
}
```

## Generic function

```ts
function identity<T>(arg: T): T {
return arg;
return arg;
}
```

Expand Down
Loading