Typecho内置的md解析/非插件实现路由/延伸:评论发布后可修改(可能会用到的
发表于 : 2019年 7月 8日 04:22
非插件实现路由出自。kotori大佬 https://kotori.love/archives/typecho-theme-tips.html
$parser = new HyperDown();
$parser->makeHtml(此处为MD内容);
因为最近在写ajax评论,评论想获取解析后的,所以翻看typecho源码找到此方法.
以下所有方法需要放在主题function.php里的themeInit下。
ajax访问生成的路由, 组成为:网址/邮箱/父评论id
数据库进行查询后按时间排序,利用内置md解析,把评论转为html(如果不开启md评论的话这步可以刨除.
这样就能制作一个很简单的评论ajax请求返回
if ($archive->is('archive', 404)){
header( "HTTP/1.1 200 OK" );
$info=explode("/",$archive->request->getPathinfo());
$db = Typecho_Db::get();
if($info[1]){
$t = $db->fetchRow($db->select('text','coid')->from('table.comments')->where('mail = ?', $info[0])->where('parent = ?', $info[1])->order('mid',Typecho_Db::SORT_DESC));
}else{
$t = $db->fetchRow($db->select('text','coid')->from('table.comments')->where('mail = ?', $info[0])->order('mid',Typecho_Db::SORT_DESC));
}
$parser = new HyperDown();
echo 'coid:'.$t['coid'].'text:'$parser->makeHtml($t['text']);
exit;
}
另一些通过非插件实现路由实现的小玩具
输出所有图片合并成json 可以用来做相册.
if ($archive->is('archive', 404)){$path_info = trim($archive->request->getPathinfo(), '/');
if(strpos($path_info,".img") > 0){ //请求判断,自己改一下就ok
$db = Typecho_Db::get();
$t = $db->fetchAll($db->select('str_value')->from('table.fields')->where('name = ?' , 'img'));
header( "HTTP/1.1 200 OK" );
foreach($t as $key=>$sc){
echo '{"img":'.$sc['str_value'].'}';
}
exit;
}}
文章的md版本小玩具
if ($archive->is('archive', 404)){$path_info = trim($archive->request->getPathinfo(), '/');
if(strpos($path_info,".md") > 0){
$right = strpos($path_info, '.');
$id=substr($path_info, 0, $right);
$db = Typecho_Db::get();
$t = $db->fetchRow($db->select('title','text')->from('table.contents')->where('cid = ?', $id));
header( "HTTP/1.1 200 OK" );
echo '<h3>'.$t['title'].'</h3>'.'<pre style="word-wrap: break-word; white-space: pre-wrap;">'.$t[text].'</pre>';
exit;
}}
对post请求处理.基础版
if ($archive->is('archive', 404)){
header( "HTTP/1.1 200 OK" );
if($archive->request->isPost()){
print_r($_POST['name']);
}
exit;
}
可以延伸出对评论成功后的可编辑,后台做好判断即可.
$parser = new HyperDown();
$parser->makeHtml(此处为MD内容);
因为最近在写ajax评论,评论想获取解析后的,所以翻看typecho源码找到此方法.
以下所有方法需要放在主题function.php里的themeInit下。
ajax访问生成的路由, 组成为:网址/邮箱/父评论id
数据库进行查询后按时间排序,利用内置md解析,把评论转为html(如果不开启md评论的话这步可以刨除.
这样就能制作一个很简单的评论ajax请求返回
if ($archive->is('archive', 404)){
header( "HTTP/1.1 200 OK" );
$info=explode("/",$archive->request->getPathinfo());
$db = Typecho_Db::get();
if($info[1]){
$t = $db->fetchRow($db->select('text','coid')->from('table.comments')->where('mail = ?', $info[0])->where('parent = ?', $info[1])->order('mid',Typecho_Db::SORT_DESC));
}else{
$t = $db->fetchRow($db->select('text','coid')->from('table.comments')->where('mail = ?', $info[0])->order('mid',Typecho_Db::SORT_DESC));
}
$parser = new HyperDown();
echo 'coid:'.$t['coid'].'text:'$parser->makeHtml($t['text']);
exit;
}
另一些通过非插件实现路由实现的小玩具
输出所有图片合并成json 可以用来做相册.
if ($archive->is('archive', 404)){$path_info = trim($archive->request->getPathinfo(), '/');
if(strpos($path_info,".img") > 0){ //请求判断,自己改一下就ok
$db = Typecho_Db::get();
$t = $db->fetchAll($db->select('str_value')->from('table.fields')->where('name = ?' , 'img'));
header( "HTTP/1.1 200 OK" );
foreach($t as $key=>$sc){
echo '{"img":'.$sc['str_value'].'}';
}
exit;
}}
文章的md版本小玩具
if ($archive->is('archive', 404)){$path_info = trim($archive->request->getPathinfo(), '/');
if(strpos($path_info,".md") > 0){
$right = strpos($path_info, '.');
$id=substr($path_info, 0, $right);
$db = Typecho_Db::get();
$t = $db->fetchRow($db->select('title','text')->from('table.contents')->where('cid = ?', $id));
header( "HTTP/1.1 200 OK" );
echo '<h3>'.$t['title'].'</h3>'.'<pre style="word-wrap: break-word; white-space: pre-wrap;">'.$t[text].'</pre>';
exit;
}}
对post请求处理.基础版
if ($archive->is('archive', 404)){
header( "HTTP/1.1 200 OK" );
if($archive->request->isPost()){
print_r($_POST['name']);
}
exit;
}
可以延伸出对评论成功后的可编辑,后台做好判断即可.