• This repository has been archived on 21/Jan/2024
  • Stars
    star
    219
  • Rank 181,133 (Top 4 %)
  • Language
    PHP
  • Created over 5 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A simple package to parse Github Flavored Markdown in PHP

GitDown - a simple package to parse markdown in PHP

GitDown

A simple package for parsing (GitHub Flavored) Markdown in PHP.

WARNING

This package is a fraud. All it does is fire off your markdown to a public GitHub API that returns the parsed result.

Knowing this, if you don't store the result, or take advantage of the provided caching features, you'll end up with slow page loads, or worse, rate-limit errors from GitHub.

Markdown parsing is super annoying, and this tradeoff is well worth it to me, I hope you embrace it as well.

Installation

composer require calebporzio/gitdown

TLDR;

// Optionally set a GitHub Personal Access Token to increase rate-limit.
GitDown::setToken($token);

GitDown::parse($markdown);

// Uses Laravel's cache()->rememberForever() under the hood.
GitDown::parseAndCache($markdown);

Optionally, add the @gitdown snippet to your template's <head> section, and a .markdown-body class to a wrapper element, for GitHub markdown/code-syntax styling.

<head>
    [...]
    @gitdown
</head>
<body>
    <div class="markdown-body">
        {!! GitDown::parseAndCache($content) !!}
    </div>
</body>

Authenticating With GitHub

Without authentication, GitHub will limit your API calls to 60 calls/hour. If you use authentication tokens, you can increase this limit to 5000 calls/minute. It is highly recommended that you use a "Personal Access Token" with this package. To obtain one, click here. (You can leave the permissions blank for this token.)

First, publish the package's config file.

php artisan vendor:publish --provider="GitDown\GitDownServiceProvider"

Then, add the following entry to your .env file.

[...]
GITHUB_TOKEN=your-token-here

Usage

GitDown::parse($markdown);

// Will be cached forever. (suggested)
GitDown::parseAndCache($markdown);

// Will be cached for 24 hours. (minutes in Laravel < 5.8, seconds otherwise)
GitDown::parseAndCache($markdown, $seconds = 86400);

// Pass in your own custom caching strategy.
GitDown::parseAndCache($markdown, function ($parse) {
    return Cache::rememberForever(sha1($markdown), function () use ($parse) {
        return $parse();
    });
});

Allowing Dangerous Tags

By default, GitHub sanitizes HTML tags it deems "unsafe" like <iframe>s. However, it's common to embed video or audio into your markdown with <iframe>s.

GitDown can intelligently preserve your tags by filling the allowedTags config array option in config/gitdown.php with the tags you want to prevent being parsed.

"allowedTags" => [
    'iframe',
],

Non-Laravel Usage

You can set a GitHub Personal Access Token by passing it into the GitDown's constructor. new GitDown\GitDown($token)

// You can pass config options into the constructur:
$gitDown = new GitDown\GitDown(
    $token = 'foo',
    $context = 'your/repo',
    $allowedTags = []
);

$gitDown->parse($markdown);

// Pass in your own custom caching strategy.
$gitDown->parseAndCache($markdown, function ($parse) {
    return Cache::rememberForever(sha1($markdown), function () use ($parse) {
        return $parse();
    });
});

Markdown/Syntax CSS

Styling markdown with CSS has always been a bit of a pain for me. Not to mention trying to style syntax inside code blocks. Not to worry!

GitDown ships with all the CSS you need to make your markdown look exactly like it does on GitHub. Just add this code somewhere on your HTML page, preferably near your other stylesheets in the <head> section.

<head>
    [...]
    @gitdown
</head>

Non-Laravel

<head>
    [...]
    <style><?php echo GitDown\GitDown::styles(); ?></style>
</head>

Bam! That's all you need to make everything look good πŸ€™.

If echoing out CSS directly on your page doesn't sit well with you, you can add the styles to your stylesheet yourself using NPM.

npm install primer-markdown github-syntax-light --save

Now you can include the SCSS files in your Sass bundler:

@import "primer-markdown/index.scss";
// The relative directories may be a little different for you.
@import "./../../node_modules/github-syntax-light/lib/github-light.css";

GitHub Flavored Markdown

To enable GFM parsing for GitDown, set the "context" entry in config/gitdown.php to a repository name.

"context" => "your/repo",

Enjoy!

Hope this makes your life easier. If it does, show the project some love on Twitter and tag me: @calebporzio

More Repositories

1

sushi

Eloquent's missing "array" driver.
PHP
2,570
star
2

awesome-helpers

Helper functions I find super-duper handy
PHP
629
star
3

onboard

A Laravel package to help track user onboarding steps.
PHP
439
star
4

better-phpunit

A better PHPUnit test runner for VS Code
TypeScript
211
star
5

laracasts-livewire-datatable

The Laravel project I used during the "Building DataTables with Livewire" Laracasts video
PHP
118
star
6

bear-sync

Access your Bear notes in Laravel
PHP
107
star
7

tailbuild

A simple command to build a Tailwind CSS file for your project (with JIT compiling and watching)
JavaScript
89
star
8

laravel-helpers-file

Because I can never remember exactly how to autoload my helpers.php file.
PHP
61
star
9

laravel-frontend-preset

My personal frontend preset for new laravel applications.
PHP
31
star
10

laracon-online-2020

The Laravel project used in my Laracon Online 2020 talk
PHP
28
star
11

laracasts-building-alpine

JavaScript
27
star
12

simple-php-cs-fixer

A VS Code extension for simple php-cs-fixer integration
PHP
24
star
13

sps

TypeScript
20
star
14

vue-form-state

JavaScript
19
star
15

vercel-laravel

PHP
19
star
16

click

The podcast recording and editing suite of your dreams. Featuring just the one button.
Crystal
18
star
17

livewire-uncovered

PHP
13
star
18

laracasts-turbolinks

The source app from my Laracasts turbolinks video.
PHP
11
star
19

usesushi.dev

The website for sushi
PHP
10
star
20

presettings

A VS Code extension for storing and activating settings presets
TypeScript
10
star
21

laracasts-server-fetched-partials

The Laravel app used in my Laracasts episode on "Server-Fetched Partials"
PHP
10
star
22

laracasts-cached-server-fetched-partials

The finished Laravel app from my "Caching Server Fetched Partials" video.
PHP
10
star
23

vue-fetch-html

A little component for fetching html/vue/javascript from the server.
Vue
9
star
24

livewire-bot

Livewire's GitHub bot
PHP
7
star
25

laravel-acceptance-example

An example laravel project with acceptance tests that render javascript and work well with laravel's helpers.
PHP
7
star
26

model-inheritance

PHP
6
star
27

99bottles-php

PHP
4
star
28

edoc-2019

PHP
4
star
29

vue-example-component

For when you regret getting rid of the default Laravel ExampleComponent.vue
Vue
3
star
30

screendit

PHP
3
star
31

calebporzio

3
star
32

johnny

HTML
2
star
33

dotfiles

Shell
2
star
34

write-less-js

PHP
1
star
35

advent-of-code-2018

My Advent Of Code solutions for 2018
PHP
1
star
36

rm-me

1
star
37

spend-mo

JavaScript
1
star
38

psrpoetry

HTML
1
star
39

ingenuity

CSS
1
star
40

ingenuity-teaser

HTML
1
star
41

theretireddevtheme

A modified ghost (blog) theme for theretireddev.com
CSS
1
star