Skip to content

Commit

Permalink
Markdown parsing and menu improvments
Browse files Browse the repository at this point in the history
- Add support for internal links
- Add support for anchor links
- Add custom directory and file 'here' css class names
  • Loading branch information
davidpede committed Nov 28, 2018
1 parent b504b03 commit 21dc832
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,27 @@
* @var array $scriptProperties
*/

/** @var GitHubDocs|null $ghd */
$ghd = $modx->getService('githubdocs', 'GitHubDocs', $modx->getOption('githubdocs.core_path', null, MODX_CORE_PATH . 'components/githubdocs/') . 'model/githubdocs/', $scriptProperties);
if (!($ghd instanceof GitHubDocs)) return $modx->log(MODX::LOG_LEVEL_ERROR, 'Service class not loaded');

//settings
$repo_owner = $modx->getOption('repoOwner', $scriptProperties, '');
$repo_name = $modx->getOption('repoName', $scriptProperties, '');
$docs_path = $modx->getOption('docsPath', $scriptProperties, '');
$private = ($modx->getOption('private', $scriptProperties) === '1') ? true : false;
$parse = ($modx->getOption('parse', $scriptProperties) !== '0') ? true : false;
$debug = ($modx->getOption('debug', $scriptProperties) === '1') ? true : false;
//templating
$scriptProperties = array_merge(array(
'base_url' => $modx->makeUrl($modx->resource->get('id')) . rtrim($docs_path,'/') . '/',
), $scriptProperties);
//cache
$cache_expires = $modx->getOption('cacheExpires', $scriptProperties, 86400); //TODO move to sys settings
$cache_opts = array(
xPDO::OPT_CACHE_KEY => ($cache_key = $modx->getOption('cacheKey', $scriptProperties)) ? 'gitHubDocs/' . $cache_key : 'gitHubDocs' //TODO move to sys settings
);

/** @var GitHubDocs|null $ghd */
$ghd = $modx->getService('githubdocs', 'GitHubDocs', $modx->getOption('githubdocs.core_path', null, MODX_CORE_PATH . 'components/githubdocs/') . 'model/githubdocs/', $scriptProperties);
if (!($ghd instanceof GitHubDocs)) return $modx->log(MODX::LOG_LEVEL_ERROR, 'Service class not loaded');

$output = '';

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,32 @@
* @var array $scriptProperties
*/

/** @var GitHubDocs|null $ghd */
$ghd = $modx->getService('githubdocs', 'GitHubDocs', $modx->getOption('githubdocs.core_path', null, MODX_CORE_PATH . 'components/githubdocs/') . 'model/githubdocs/', $scriptProperties);
if (!($ghd instanceof GitHubDocs)) return $modx->log(MODX::LOG_LEVEL_ERROR, 'Service class not loaded');

//settings
$repo_owner = $modx->getOption('repoOwner', $scriptProperties, '');
$repo_name = $modx->getOption('repoName', $scriptProperties, '');
$docs_path = $modx->getOption('docsPath', $scriptProperties, '');
$private = ($modx->getOption('private', $scriptProperties) === '1') ? true : false;
$debug = ($modx->getOption('debug', $scriptProperties) === '1') ? true : false;
//templates
//templating
$file_tpl = $modx->getOption('fileTpl', $scriptProperties, '');
$dir_tpl = $modx->getOption('dirTpl', $scriptProperties, '');
$scriptProperties = array_merge(array(
'base_url' => $modx->makeUrl($modx->resource->get('id')) . rtrim($docs_path,'/') . '/',
), $scriptProperties);
//cache
$cache_expires = $modx->getOption('cacheExpires', $scriptProperties, 86400); //TODO move to sys settings
$cache_opts = array(
xPDO::OPT_CACHE_KEY => ($cache_key = $modx->getOption('cacheKey', $scriptProperties)) ? 'gitHubDocs/' . $cache_key : 'gitHubDocs'
);

/** @var GitHubDocs|null $ghd */
$ghd = $modx->getService('githubdocs', 'GitHubDocs', $modx->getOption('githubdocs.core_path', null, MODX_CORE_PATH . 'components/githubdocs/') . 'model/githubdocs/', $scriptProperties);
if (!($ghd instanceof GitHubDocs)) return $modx->log(MODX::LOG_LEVEL_ERROR, 'Service class not loaded');

$output = '';

try {
if ($repo_owner && $repo_name) {
if ($repo_owner && $repo_name && $docs_path) {
//request
if (!$tree = $modx->cacheManager->get(md5($repo_name . $docs_path) . '_tree', $cache_opts)) {
$dir = $ghd->getDirectory($repo_owner, $repo_name,'docs', $private);
Expand All @@ -41,8 +44,7 @@
//menu templating
if ($dir_tpl && $file_tpl) {
//links
$base_url = $modx->makeUrl($modx->resource->get('id')) . $docs_path . '/';
$menu = $ghd->templateTree($tree, $base_url, $dir_tpl, $file_tpl, $debug, $_REQUEST);
$menu = $ghd->templateTree($tree, $dir_tpl, $file_tpl, $debug, $_REQUEST);
//output
if ($debug) {
$output = '<pre>' . print_r($menu, true) . '</pre>';
Expand All @@ -53,11 +55,11 @@
throw new Exception('Snippet [[ghdGetMenu]] requires &dirTpl and &fileTpl parameters.');
}
} else {
throw new Exception('Snippet [[ghdGetMenu]] requires &repoOwner and &repoName parameters.');
throw new Exception('Snippet [[ghdGetMenu]] requires &repoOwner, &repoName and &docsPath parameters.');
}
} catch (\GuzzleHttp\Exception\GuzzleException | Exception $e) {
$ghd->exceptionHandler($e);
return;
}

return $output;
return $output;
57 changes: 44 additions & 13 deletions core/components/githubdocs/model/githubdocs/githubdocs.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,22 @@ public function getTree(string $repo_owner, string $repo_name, string $sha, bool
* Recursively template a nested array generated from getTree().
*
* @param array $array - An array generated by getDirTree()
* @param string $base_url - Used for link building
* @param string $dir_tpl - Chunk to use as a template
* @param string $file_tpl - Chunk to use as a template
* @param bool $debug - Debugging flag
* @param array $request - $_REQUEST array
*
* @return array
* @throws Exception
*/
public function templateTree(array $array, string $base_url, string $dir_tpl = '', string $file_tpl = '', bool $debug = false, array $request = array())
public function templateTree(array $array, string $dir_tpl = '', string $file_tpl = '', bool $debug = false, array $request = array())
{
$output = array();
$node['class'] = '';
$base_url = $this->config['base_url'] ?? '/';
$dir_cls = $this->config['hereDirCls'] ?? 'active';
$file_cls = $this->config['hereFileClass'] ?? 'active';

foreach ($array as $node) {
//global
$node['title'] = $this->cleanTitle($node['name']);
Expand All @@ -155,17 +159,17 @@ public function templateTree(array $array, string $base_url, string $dir_tpl = '
switch ($node['type']) {
case 'tree':
if (in_array($node['name'], $request)) {
$node['class'] = 'nav-item-expanded nav-item-open active';
$node['class'] = $dir_cls;
}
$node['children'] = $this->templateTree($node['children'], $base_url, $dir_tpl, $file_tpl, $debug, $request);
$node['children'] = $this->templateTree($node['children'], $dir_tpl, $file_tpl, $debug, $request);
if ($dir_tpl && !$debug) {
$node['children'] = implode("\n", $node['children']);
$node = $this->modx->getChunk($dir_tpl, $node);
}
break;
case 'blob':
if (in_array(str_replace('.md', '', $node['name']), $request)) {
$node['class'] = 'active';
$node['class'] = $file_cls;
}
if ($file_tpl && !$debug) {
$node = $this->modx->getChunk($file_tpl, $node);
Expand Down Expand Up @@ -216,7 +220,7 @@ public function parseMarkDown(string $md = null)
{
try {
$parse = new ParsedownExtra();
$html = $parse->text(base64_decode($md));
$html = $this->cleanHtml($parse->text(base64_decode($md)));
return $html;
} catch (Exception $e) {
throw $e;
Expand All @@ -241,13 +245,13 @@ public function cleanUrl(string $url)
}

/**
* Returns a clean title.
*
* @param string $title - url or path to clean
*
* @return string
* @throws Exception
*/
* Returns a clean title.
*
* @param string $title - url or path to clean
*
* @return string
* @throws Exception
*/
public function cleanTitle(string $title)
{
try {
Expand All @@ -265,6 +269,33 @@ public function cleanTitle(string $title)
}
}

/**
* Returns a parsed html string with clean internal links.
*
* @param string $html - string to clean
*
* @return string
* @throws Exception
*/
public function cleanHtml(string $html)
{
try {
$base_url = $this->config['base_url'] ?? '/';
$referer = $_REQUEST['q'] ?? '';
return str_replace(array(
'href="/',
'href="#',
'.md">'
), array(
'href="' . $base_url,
'href="' . $referer . '#',
'.html">',
), $html);
} catch (Exception $e) {
throw $e;
}
}

/**
* Custom exception handler
*
Expand Down

0 comments on commit 21dc832

Please sign in to comment.