当前位置: 首页 > 工具软件 > DokuWiki > 使用案例 >

安装DokuWiki集成markdown过程

红富
2023-12-01

最近我们需要用dokuwiki来做知识库

安装php环境

使用宝塔面板进行安装centos安装地址 https://www.bt.cn
安装命令 yum install -y wget && wget -O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh
安装好后,在软件商店安装nginx和php7.3版本。

安装dokuwiki

在/www/wwwroot下面执行git clone https://github.com/splitbrain/dokuwiki.git
chown -R www:www dokuwiki
在宝塔面板网址电机新建网址输入
域名:ip地址:port
备注: dokuwiki
目录:选择目录
最后输入http://ip:port/install.php

集成markdown

在插件管理器里面安装如下插件
1、Add New Page
2、Markdown Page Plugin
3、Indexmenu Plugin
配置代码如下
新建/data/pages/sidebar.txt

===== 导航目录 =====
  {{indexmenu>..|navbar}}
===== 添加新页面 =====
{{NEWPAGE}}

dokuwiki建立中文词条文件乱码
1、dokuwiki/conf/local.php
2、dokuwiki/Inc/pageutils.php

$conf['fnencode']='utf-8';
function utf8_encodeFN($file,$safe=true){
    global $conf;
    if($conf['fnencode'] == 'utf-8') return $file;
    if($safe && preg_match('#^[a-zA-Z0-9/_\-\.%]+$#',$file)){
        return $file;
    }
    if($conf['fnencode'] == 'safe'){
        return SafeFN::encode($file);
    }
    // 中文支持
    if ($conf['fnencode']=='gb2312'){
        return iconv('UTF-8','GB2312',$file);
    }
    $file = urlencode($file);
    $file = str_replace('%2F','/',$file);
    return $file;
}

function utf8_decodeFN($file){
    global $conf;
    if($conf['fnencode'] == 'utf-8') return $file;

    if($conf['fnencode'] == 'safe'){
        return SafeFN::decode($file);
    }
    // 中文支持
    if ($conf['fnencode']=='gb2312'){
        return iconv('GB2312','UTF-8',$file);
    }
    return urldecode($file);
}

接下来下载editor.md
1、https://github.com/pandao/editor.md/archive/v1.5.0.tar.gz
2、解压到dokuwiki/lib/下面
3、修改dokuwiki/inc/form.php
4、修改dokuwiki/inc/parser/xhtml.php
5、修改dokuwiki/inc/Action/Save.php
6、修改dokuwiki/lib/tpl/dokuwiki/main.php
7、新增uploadimg.php到dokuwiki目录
8、修改lib/editor.md/lib/marked.min.js

function form_wikitext($attrs) {
    // mandatory attributes
    unset($attrs['name']);
    unset($attrs['id']);
 	$text = str_replace("<markdown>\n",'',$attrs['_text']);
    $text = str_replace("\n</markdown>",'',$text);
    return '<div id="editormd" contenteditable="true"><textarea name="wikitext">'.DOKU_LF.formText($text).'</textarea></div>';                
}
function cdata($text) {
     return $this->doc.=$text;
}
saveWikiText($ID,con($PRE,$TEXT,$SUF,true),$SUM,$INPUT->bool('minor')); //use pretty mode for con
替换成
saveWikiText($ID,con($PRE,"<markdown>\n".$TEXT."\n</markdown>",$SUF,true),$SUM,$INPUT->bool('minor')); //use pretty mode for con
<link rel="stylesheet" href="<?php echo DOKU_BASE;?>lib/editor.md/css/editormd.min.css" />
	<script src="<?php echo DOKU_BASE;?>lib/editor.md/examples/js/jquery.min.js"></script>
    <script src="<?php echo DOKU_BASE;?>lib/editor.md/editormd.js"></script>
    <script type="text/javascript">
    $ = jQuery
        var testEditor;
        $(function(md) {
        if (document.getElementById("editormd")) {
            testEditor = editormd("editormd", {
                width: "100%",
                height: 740,
                path: '<?php echo DOKU_BASE;?>lib/editor.md/lib/',
                syncScrolling : "single",
                imageUpload: true,
                imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
                imageUploadURL: "<?php echo DOKU_BASE;?>uploadimg.php",
                onload: function() {}
            });
        }
        });

        window.onload = function() {
            //document.getElementById("editormd").addEventListener('paste', function (event) {
            $("#editormd").on('paste', function(event) {
                //console.log(event);
                var items = (event.clipboardData || event.originalEvent.clipboardData).items;
                for (var index in items) {
                    var item = items[index];
                    //console.log(item);
                    if (item.kind === 'file') {
                        var blob = item.getAsFile();                        
                        var reader = new FileReader();
                        reader.onload = function(event) {                            
                            var base64 = event.target.result;
                            console.log(base64);
                            //ajax上传图片
                            $.post("<?php echo DOKU_BASE;?>uploadimg.php", {
                                screenshots: base64
                            }, function(rets) {
                                ret = JSON.parse(rets);
                                //layer.msg(ret.msg);
                                console.log(ret);
                                if (ret.success === 1) {
                                    //新一行的图片显示
                                    testEditor.insertValue("\n![](" + ret.url + ")");
                                } else {
                                    alert("截图上传失败:" + ret.message);
                                }
                            });
                        };
                        reader.readAsDataURL(blob);
                    }
                }
            });
        }
    </script>
<?php
if(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__).'/');
require_once(DOKU_INC.'inc/init.php');

$INFO = pageinfo();
$hostpath=getBaseURL(false);
$attachDir='/data/media/editor/';//上传文件保存路径,结尾不要带/
$maxAttachSize = 2*1024*1024;  //最大上传大小,默认是2M

function upEditorImg(){
    global $hostpath, $attachDir, $maxAttachSize;
    //获取文件的大小
    $file_size=$_FILES['editormd-image-file']['size'];
    //echo "$file_size $maxAttachSize";
    if($file_size > $maxAttachSize) {
        echo '{"success":0,"message":"不能上传大于2M的文件"}';
        return false;
    }

    //获取文件类型
    $file_type=$_FILES['editormd-image-file']['type'];
    if($file_type!="image/jpeg" && $file_type!='image/pjpeg' && $file_type!="image/png") {
        echo '{"success":0,"message":"图片格式异常"}';
        return false;
    }

    //判断是否上传成功(是否使用post方式上传)
    if(is_uploaded_file($_FILES['editormd-image-file']['tmp_name'])) {
        //把文件转存到你希望的目录(不要使用copy函数)
        $uploaded_file=$_FILES['editormd-image-file']['tmp_name'];

        //我们给每个用户动态的创建一个文件夹
        $save_path=$_SERVER['DOCUMENT_ROOT'].$hostpath.$attachDir;
        //判断该用户文件夹是否已经有这个文件夹
        if(!file_exists($save_path)) {
            mkdir($save_path);
        }

        //$move_to_file=$save_path."/".$_FILES['editormd-image-file']['name'];
        $file_true_name=$_FILES['editormd-image-file']['name'];
        $move_file_name=time().rand(1,1000).substr($file_true_name,strrpos($file_true_name,"."));
        $move_to_file=$save_path.$move_file_name;
        //echo "$uploaded_file   $move_to_file";
        if(move_uploaded_file($uploaded_file,iconv("utf-8","gb2312",$move_to_file))) {
            //echo $_FILES['editormd-image-file']['name']."上传成功";
            //echo '{"success":1,"message":"上传成功", "url":"'.$hostpath.$attachDir.$move_file_name.'"}';
            $result=array(
              'success'=> 1,
              'message'=>'上传成功',
              'url'=>'editor:'.$move_file_name
            );
            echo json_encode($result);
        } else {
            //echo "上传失败";
            echo '{"success":0,"message":"服务器保存文件失败"}';
        }
    } else {
        //echo "上传失败";
        echo '{"success":0,"message":"上传失败"}';
        return false;
    }
}

//$_POST= [screenshots] => data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAI0AAACcCAYAAABC1CibAAAL6UlEQVR4Ae2dbUiU6RrH...
function upEditorScreenshots(){
    global $hostpath, $attachDir, $maxAttachSize;

    $content = $_POST['screenshots'];

    if (preg_match('/^data:image\/(\w+);base64,(\S+)/', $content, $result)) {
        $file_type = $result[1];
        $base64data = $result[2];

        //echo "$file_type $base64data";
        $save_path = $_SERVER['DOCUMENT_ROOT'].$hostpath.$attachDir;
        if (!is_dir($save_path)) {
            mkdir($save_path, 0777);
        }

        $filedata = base64_decode($base64data);
        $filename = time().rand(1,1000).".".$file_type;
        if (!file_put_contents($save_path . $filename, $filedata)) {
            echo '{"success":0,"message":"服务器保存文件失败"}';
            return false;
        }
        unset($filedata);

        echo '{"success":1,"message":"上传成功", "url":"editor:'.$move_file_name.$filename.'"}';
        return true;
    } else {
        echo '{"success":0,"message":"图片格式异常"}';
        return false;
    }
}

//print_r($_POST);
//print_r($_FILES);

if(isset($_FILES['editormd-image-file'])){
    global $INFO;
    if($INFO['writable'] && !$INFO['locked']) {
        upEditorImg();
    } else {
    echo '{"success":0,"message":"没有权限"}';
    }
    exit();
}

if(isset($_POST['screenshots'])){
    global $INFO;
    if($INFO['writable'] && !$INFO['locked']) {
        upEditorScreenshots();
    } else  {
    echo '{"success":0,"message":",没有权限"}';
    }
    exit();
}
?>
 if(href.indexOf(':')>0&&href.indexOf("/")<0){href='/lib/exe/fetch.php?cache=&media='+href;}
 类似资料: