我正在使用Graham Campbell的markdown parser作为Laravel在我正在建造的应用程序中。虽然它确实将Markdown解析为HTML,但它实际上是返回标签,而不是我正在寻找的格式化文本。以下是有问题的观点:
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class TestClass extends Panel {
/**
*
*/
private static final long serialVersionUID = 1L;
Panel ballPanel = new Panel();
Label ballLabel = new Label();
Panel coordinatePanel = new Panel();
Label coordinateLabel = new Label();
int nowX, nowY;
Label nowXcoordinateLabel = new Label("Now X :");
Label nowYcoordinateLabel = new Label("Now Y :");
TestClass() {
coordinatePanel.add(coordinateLabel);
nowXcoordinateLabel.setBackground(Color.WHITE);
nowYcoordinateLabel.setBackground(Color.WHITE);
nowXcoordinateLabel.setPreferredSize(new Dimension(100, 25));
nowYcoordinateLabel.setPreferredSize(new Dimension(100, 25));
coordinatePanel.setPreferredSize(new Dimension(400, 30));
coordinatePanel.setBackground(Color.LIGHT_GRAY);
coordinatePanel.add(nowXcoordinateLabel);
coordinatePanel.add(nowYcoordinateLabel);
MouseAdapter ml = new MouseAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
nowXcoordinateLabel.setText("Now X :" + e.getX());
nowYcoordinateLabel.setText("Now Y :" + e.getY());
nowX = e.getX();
nowY = e.getY();
repaint();
super.mouseMoved(e);
}
};
setLayout(new GridLayout(1, 1));
setBackground(Color.WHITE);
addMouseMotionListener(ml);
setVisible(true);
setSize(400, 400);
}
@Override
public void paint(Graphics g) {
Graphics2D gg = (Graphics2D) g;
gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gg.setColor(Color.BLUE);
gg.fillOval(nowX, nowY, 20, 20);
}
public static void main(String[] arg) {
TestClass mb = new TestClass();
Frame frame = new Frame("Test drawing");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
frame.dispose();
super.windowClosing(e);
}
});
frame.setLayout(new GridLayout(1, 1));
frame.add(mb);
frame.add(mb.coordinatePanel);
frame.setSize(800, 600);
frame.setVisible(true);
}
}
我最初的怀疑是str_limit方法与它有关,但改变操作的顺序甚至完全删除str_limit都没有效果。因此,例如,目前,如果我的帖子有正文@extends('layouts.app')
@section('stylesheet')
<link rel="stylesheet" href="{{ asset('css/writing.css') }}">
@endsection
@php
use GrahamCampbell\Markdown\Facades\Markdown;
@endphp
@section('content')
<h2 class="writing_title">writing</h2>
<h4 class="writing_subtitle">musings & guides</h4>
<hr class="divider">
<section class="writing_listing">
@foreach( $posts as $post )
<article class="writing_article">
<div class="writing_article-title_block">
<h3 class="writing_article-title">{{ $post->title }}</h3>
<span class="writing_article-datetime">{{ date('M j, Y H:i', strtotime($post->created_at)) }}</span>
</div>
<div class="writing_article-content_block">
{{ str_limit(Markdown::convertToHtml($post->body), 250) }}
</div>
<a href="{{ url('writing/'.$post->slug) }}">Read More</a>
</article>
@endforeach
</section>
@endsection
,那么我将取回的字面意思是# Header of post
字符串而不是帖子。我的所有段落都以<h1>Header of post</h1>
作为字符串限定。
答案 0 :(得分:1)
问题最有可能出现在这里:
{{ str_limit(Markdown::convertToHtml($post->body), 250) }}
laravel中的双括号({{ }}
)将被清理,这将使您的代码作为实际标记而不是格式化输出。你能做的就是替换它:
{{ str_limit(Markdown::convertToHtml($post->body), 250) }}
用这个:
{!! str_limit(Markdown::convertToHtml($post->body), 250) !!}
此语法将未经过验证的html回显到您的模板中。
希望这有帮助!欢呼声。