Skip to main content

Code Examples

BookStack uses Codemirrror to render code blocks with syntax highlighting. Below are various examples of this:

PHP

/**
 * Get a path to a theme resource.
 */
function theme_path(string $path = ''): ?string
{
    $theme = config('view.theme');

    if (!$theme) {
        return null;
    }

    return base_path('themes/' . $theme . ($path ? DIRECTORY_SEPARATOR . $path : $path));
}

JavaScript

performReplacements(string, replacements) {
    if (!replacements) return string;
    const replaceMatches = string.match(/:([\S]+)/g);
    if (replaceMatches === null) return string;
    replaceMatches.forEach(match => {
        const key = match.substring(1);
        if (typeof replacements[key] === 'undefined') return;
        string = string.replace(match, replacements[key]);
    });
    return string;
}

Bash / Shell

# Get our images
for i in $(seq 1 $COUNT)
do
    echo "Fetching image ${i} of ${COUNT}"
    curl -sL "https://source.unsplash.com/random/${WIDTH}x${HEIGHT}?${TOPIC}" > "${PREFIX}image_${WIDTH}x${HEIGHT}_$i.jpg"
    sleep 0.6
done

HTML

<div back-to-top class="primary-background print-hidden">
  <div class="inner">
     <span>Back to top</span>
  </div>
</div>

Nginx

server {
  listen 80;
  listen [::]:80;

  server_name bookstack.dev;

  root /var/www/bookstack/public;
  index index.php index.html;

  location / {
    try_files $uri $uri/ /index.php?$query_string;
  }
  
  location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
  }
}

Markdown

### 📜 Code Standards

PHP code style is enforced automatically [using StyleCI](https://github.styleci.io/repos/41589337). 
If submitting a PR, any formatting changes to be made will be automatically fixed after merging.