This article is mainly to share with you PHP markdown Document management tool implementation code, hope to help everyone.
Main objectives:
1. Can be edited by multiple people
2. You can view it in the browser
3 A directory that can be collapsed
4. Support multi-level directory
5. Support Markdown
6. Quick and easy
After work has been engaged in PHP development, from the previous embracing to the present back to the service-side write interface, which is exposed to a few interface document management tools or systems, briefly described below:
Showdoc, full-featured and concise, with users, rights management functions, support markdown, support export word, there are a variety of document templates, directory support two-level folding
Confluence, powerful (Rights management, email alerts, full-text search, plug-in management, etc.), heavy, charge of a document management system
Swagger, you need to write a lot of comments in your code to match
Readmine, feature-rich similar to confluence, its documents are saved in TXT, can be traced back to change, can be full-text search, but writing documents a bit painful, suitable for task/bug tracking management, etc.
Gitbook, Nodejs installation, support markdown, support NPM plug-in, the left of the collapsible directory tree will need to install plug-ins, you can also install search plugins, directories are separate markdown files, I use the feeling from MD to HTML compilation too slow (600+ document, To compile more than 25 minutes, if you have incremental compilation or improve the speed of the plug-in also please enlighten
Workaround:
It can be implemented in combination with Git, which can also take advantage of GIT's rights management capabilities
Need to compile markdown into an HTML file to deploy to the intranet
Because to be viewed in the browser, this is the end of the selection of simple access, refreshing interface, no dependency on the dtree.js (not dependent on jquery)
This function uses the tree's post-root sequence traversal algorithm to achieve a multi-level file read (no recursion, worried about writing to go around), just dtree.js also support multi-level directory folding
Here I finally chose the Segmentfault official PHP Compiler tool Class (because they have reference to optimize a lot of similar PHP classes, thanks ~)
Faster, compile more than 600 files, php time is around 2s, acceptable, and support incremental compilation; Convenient, mainly reflected in the directory is automatically generated, do not need to write the directory alone
Among the problems encountered:
Incremental compilation
Just start to determine whether an MD file needs to be compiled into HTML, is to take the MD file creation time and the last modification time to do a comparison to judge,
But later found that some of the copied, renamed files will not work with this method.
Finally, an intermediate file is used to record the time of the compiled file, and then compare with Max (creation time, last modified time) to determine if a compilation is required.
Delete redundant files
During subsequent use, it was found that some MD documents were deleted, but the final compiled files were not deleted automatically.
Therefore, at compile time, the MD file and the final HTML file will sit a difference, delete those extra HTML files
Integrated Dtree.js
First, dtree.js requires a certain amount of JSON data to display the table of contents and to expand and collapse the interaction
Also, dtree.js font is relatively small, his picture, style, script files are relative paths, I have made the corresponding changes to the path, so that it is based on the absolute path of the current domain name, so that the deployment to different domain name is not modified dtree.js code of the hierarchy directory
Assemble, beautify HTML
The assembly is written in advance of the HTML header, bottom, sidebar, and other HTML code, and then to the content of the post-compilation content, and then put it in the corresponding folder
Landscaping, this is mainly because the Segmentfault official compiler tool does not add a style to the HTML elements (for example, table, code), and I find here GitHub's relevant CSS style has been beautified
Multi-level directory support
It also cost me a lot of brain cells to write, when the university wrote dynamic Huffman code when the tree was written over the traversal,
This thought is very familiar, who knows to toss until 3 o'clock in the night to finally write well, this function is also considered one of the core components.