您看到的HTML仅仅是一个预览,因此将它存储在数据库中并不是一个好主意,因为当您尝试编辑时会遇到问题。因为HTML只是一种解释,所以同时存储两个版本(markdown和HTML)也不是一个好主意,并且在编辑和保持两个版本同步时也会遇到同样的问题。
所以最好的想法是将markdown存储在db中,然后在显示之前将其转换为服务器端。
您可以使用PHP Markdown来达到此目的。然而,这并不是你在javascript方面看到的100%完美转换,可能需要一些调整。
Stack Exchange network使用的版本是C#实现,应该有一个您使用wmd版本下载的python实现。
我调整的一件事是新线条的渲染方式,所以我在markdown.php中将其更改为将一些新行转换为
,从我在以下版本中的第626行开始:
var $span_gamut = array(
#
# These are all the transformations that occur *within* block-level
# tags like paragraphs, headers, and list items.
#
# Process character escapes, code spans, and inline HTML
# in one shot.
"parseSpan" => -30,
# Process anchor and image tags. Images must come first,
# because ![foo][f] looks like an anchor.
"doImages" => 10,
"doAnchors" => 20,
# Make links out of things like ``
# Must come after doAnchors, because you can use < and >
# delimiters in inline links like [this]().
"doAutoLinks" => 30,
"encodeAmpsAndAngles" => 40,
"doItalicsAndBold" => 50,
"doHardBreaks" => 60,
"doNewLines" => 70,
);
function runSpanGamut($text) {
#
# Run span gamut tranformations.
#
foreach ($this->span_gamut as $method => $priority) {
$text = $this->$method($text);
}
return $text;
}
function doNewLines($text) {
return nl2br($text);
}