using ZelWiki.Engine.Library;
using ZelWiki.Engine.Library.Interfaces;
using ZelWiki.Library;
using ZelWiki.Models;
using ZelWiki.Repository;
using Constants = ZelWiki.Engine.Library.Constants;
namespace ZelWiki.Engine.Implementation
{
///
/// 内链处理.
///
public class InternalLinkHandler : IInternalLinkHandler
{
///
///
///
///
///
///
///
///
///
///
///
public HandlerResult Handle(IZelEngineState state, NamespaceNavigation pageNavigation,
string pageName, string linkText, string? image, int imageScale)
{
var page = PageRepository.GetPageRevisionByNavigation(pageNavigation);
if (page == null)
{
if (state.Session?.CanCreate == true)
{
if (image != null)
{
string href;
if (image.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase)
|| image.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase))
{
//外部图片.
href =
$"
";
}
else if (image.Contains('/'))
{
//图像位于另一页面.
href =
$"
";
}
else
{
//图像位于此页面上,但此页面不存在.
href =
$"{linkText}";
}
return new HandlerResult(href)
{
Instructions = [Constants.HandlerResultInstruction.DisallowNestedProcessing]
};
}
else if (linkText != null)
{
var href =
$"{linkText}"
+ "?";
return new HandlerResult(href)
{
Instructions = [Constants.HandlerResultInstruction.DisallowNestedProcessing]
};
}
else
{
throw new Exception("No link or image was specified.");
}
}
else
{
//该页面不存在,用户没有创建该页面的权限.
if (image != null)
{
string mockHref;
if (image.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase)
|| image.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase))
{
//外部图像.
mockHref = $"
";
}
else if (image.Contains('/'))
{
//图像位于另一页.
mockHref =
$"
";
}
else
{
//图像位于此页面上,但此页面不存在.
mockHref = $"linkText";
}
return new HandlerResult(mockHref)
{
Instructions = [Constants.HandlerResultInstruction.DisallowNestedProcessing]
};
}
else if (linkText != null)
{
return new HandlerResult(linkText)
{
Instructions = [Constants.HandlerResultInstruction.DisallowNestedProcessing]
};
}
else
{
throw new Exception("未指定链接或图像.");
}
}
}
else
{
string href;
if (image != null)
{
if (image.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase)
|| image.StartsWith("https://", StringComparison.InvariantCultureIgnoreCase))
{
//外部图像.
href =
$"
";
}
else if (image.Contains('/'))
{
//图像在另一页面.
href =
$"
";
}
else
{
//图像在此页面
href =
$"
";
}
}
else
{
//内链
href = $"{linkText}";
}
return new HandlerResult(href)
{
Instructions = [Constants.HandlerResultInstruction.DisallowNestedProcessing]
};
}
}
}
}