Optimize applySlug (#1684)

* Optimize applySlug

* optimize code

* fix: more rigorous judgment

* improve slug generating

---------

Co-authored-by: joyqi <joyqi@segmentfault.com>
This commit is contained in:
Lu Fei
2024-01-05 15:55:34 +08:00
committed by GitHub
parent 9396eef2f9
commit 9635a7a0ba
4 changed files with 16 additions and 11 deletions

View File

@@ -38,7 +38,7 @@ $(document).ready(function () {
preview = $('<div id="wmd-preview" class="wmd-hidetab" />').insertAfter('.editor');
let isFullScreen = false;
const options = {}, isMarkdown = <?php echo intval($content->isMarkdown || !$content->have()); ?>;
const options = {}, isMarkdown = <?php echo json_encode(!$content->have() || $content->isMarkdown); ?>;
options.strings = {
bold: '<?php _e('加粗'); ?> <strong> Ctrl+B',

View File

@@ -493,12 +493,12 @@ EOF;
* @access public
*
* @param string|null $str 需要生成缩略名的字符串
* @param string|null $default 默认的缩略名
* @param string $default 默认的缩略名
* @param integer $maxLength 缩略名最大长度
*
* @return string
*/
public static function slugName(?string $str, ?string $default = null, int $maxLength = 128): ?string
public static function slugName(?string $str, string $default = '', int $maxLength = 128): string
{
$str = trim($str ?? '');

View File

@@ -156,7 +156,7 @@ class Contents extends Base implements QueryInterface, RowFilterInterface, Prima
/** 更新缩略名 */
if ($insertId > 0) {
$this->applySlug(!isset($rows['slug']) || strlen($rows['slug']) === 0 ? null : $rows['slug'], $insertId);
$this->applySlug(!isset($rows['slug']) || strlen($rows['slug']) === 0 ? null : $rows['slug'], $insertId, $insertStruct['title']);
}
return $insertId;
@@ -167,10 +167,11 @@ class Contents extends Base implements QueryInterface, RowFilterInterface, Prima
*
* @param string|null $slug 缩略名
* @param mixed $cid 内容id
* @param string $title 标题
* @return string
* @throws Exception
*/
public function applySlug(?string $slug, $cid): string
public function applySlug(?string $slug, $cid, string $title = ''): string
{
if ($cid instanceof Query) {
$cid = $this->db->fetchObject($cid->select('cid')
@@ -178,6 +179,10 @@ class Contents extends Base implements QueryInterface, RowFilterInterface, Prima
}
/** 生成一个非空的缩略名 */
if ((!isset($slug) || strlen($slug) === 0) && preg_match_all("/\w+/", $title, $matches)) {
$slug = implode('-', $matches[0]);
}
$slug = Common::slugName($slug, $cid);
$result = $slug;
@@ -185,11 +190,10 @@ class Contents extends Base implements QueryInterface, RowFilterInterface, Prima
$draft = $this->db->fetchObject($this->db->select('type', 'parent')
->from('table.contents')->where('cid = ?', $cid));
if ('_draft' == substr($draft->type, - 6) && $draft->parent) {
if (preg_match("/_draft$/", $draft->type) && $draft->parent) {
$result = '@' . $result;
}
/** 判断是否在数据库中已经存在 */
$count = 1;
while (

View File

@@ -577,13 +577,14 @@ trait EditTrait
$realId = 0;
/** 是否是从草稿状态发布 */
$isDraftToPublish = preg_match("/_draft$/", $this->type);
$isBeforePublish = ('publish' == $this->status);
$isAfterPublish = ('publish' == $contents['status']);
$isDraftToPublish = false;
$isBeforePublish = false;
$isAfterPublish = 'publish' === $contents['status'];
/** 重新发布现有内容 */
if ($this->have()) {
$isDraftToPublish = preg_match("/_draft$/", $this->type);
$isBeforePublish = 'publish' === $this->status;
/** 如果它本身不是草稿, 需要删除其草稿 */
if (!$isDraftToPublish && $this->draft) {