-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Documentation: Parsing markdown blocks (code blocks)
- Loading branch information
1 parent
e03ce21
commit f9e8caa
Showing
11 changed files
with
168 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import React, { Component } from 'react'; | ||
import { Link } from 'react-router-dom'; | ||
import Prism from 'prismjs'; | ||
|
||
import { getNextStory, getPreviousStory } from 'glutenberg'; | ||
import markdown from '../markdown'; | ||
|
||
class Tabs extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
this.state = { | ||
activeTab: 0, | ||
}; | ||
} | ||
|
||
selectTab( index ) { | ||
return () => { | ||
this.setState( { activeTab: index } ); | ||
}; | ||
} | ||
|
||
componentDidUpdate() { | ||
Prism.highlightAll(); | ||
} | ||
|
||
render() { | ||
const { tabs } = this.props; | ||
const activeTab = tabs[ this.state.activeTab ]; | ||
|
||
return ( | ||
<div> | ||
<div> | ||
{ tabs.map( ( tab, index ) => ( | ||
<button | ||
key={ index } | ||
onClick={ this.selectTab( index ) } | ||
className={ index === this.state.activeTab ? 'is-active' : '' } | ||
> | ||
{ tab.name } | ||
</button> | ||
) ) } | ||
{ activeTab && <div dangerouslySetInnerHTML={ { __html: activeTab.content } } /> } | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
function MarkdownContent( { content } ) { | ||
const blocks = markdown( content ); | ||
|
||
return ( | ||
<div> | ||
{ blocks.map( ( block, index ) => { | ||
if ( block.type === 'raw' ) { | ||
return <div key={ index } dangerouslySetInnerHTML={ { __html: block.content } } />; | ||
} | ||
if ( block.type === 'codetabs' ) { | ||
return <Tabs key={ index } tabs={ block.tabs } />; | ||
} | ||
|
||
return null; | ||
} ) } | ||
</div> | ||
); | ||
} | ||
|
||
class Page extends Component { | ||
componentDidMount() { | ||
Prism.highlightAll(); | ||
} | ||
|
||
render() { | ||
const { story } = this.props; | ||
const nextStory = getNextStory( story.id ); | ||
const previousStory = getPreviousStory( story.id ); | ||
|
||
return ( | ||
<div> | ||
{ !! story.Component && <story.Component /> } | ||
{ !! story.markdown && <MarkdownContent content={ story.markdown } /> } | ||
|
||
<div className="navigation"> | ||
{ !! previousStory && ( | ||
<p className="nav-older"> | ||
<Link to={ previousStory.path }>{ '←' } { previousStory.title }</Link> | ||
</p> | ||
) } | ||
{ !! nextStory && ( | ||
<p className="nav-newer"> | ||
<Link to={ nextStory.path }>{ nextStory.title } { '→' }</Link> | ||
</p> | ||
) } | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default Page; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import MarkdownIt from 'markdown-it'; | ||
import { compact } from 'lodash'; | ||
|
||
const parser = new MarkdownIt(); | ||
|
||
const blockParsers = { | ||
raw( content ) { | ||
return { | ||
type: 'raw', | ||
content: parser.render( content ), | ||
}; | ||
}, | ||
|
||
codetabs( content ) { | ||
const tabsRegex = /{%\s+([\w]+)\s+%}/gm; | ||
const splittedTabs = compact( content.trim().split( tabsRegex ) ); | ||
const tabs = []; | ||
for ( let i = 0; i < splittedTabs.length; i = i + 2 ) { | ||
tabs.push( { | ||
name: splittedTabs[ i ], | ||
content: parser.render( splittedTabs[ i + 1 ] ), | ||
} ); | ||
} | ||
|
||
return { | ||
type: 'codetabs', | ||
tabs, | ||
}; | ||
}, | ||
}; | ||
|
||
function parse( markdown ) { | ||
const blocksRegex = /({%\s+[\w]+\s+%}(?:.|\n|\r)*?{%\s+end\s+%})/gm; | ||
const blockRegex = /{%\s+([\w]+)\s+%}((?:.|\n|\r)*?){%\s+end\s+%}/gm; | ||
const blocks = markdown.split( blocksRegex ); | ||
return blocks | ||
.map( ( block ) => { | ||
const matches = blockRegex.exec( block ); | ||
if ( ! matches ) { | ||
return { type: 'raw', content: block }; | ||
} | ||
return { type: matches[ 1 ], content: matches[ 2 ] }; | ||
} ) | ||
.map( ( block ) => { | ||
const blockParser = blockParsers[ block.type ] || blockParsers.raw; | ||
|
||
return blockParser( block.content ); | ||
} ); | ||
} | ||
|
||
export default parse; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters