<?php
/**
 * 友情链接
 *
 * @package custom
 */

if (!defined('__TYPECHO_ROOT_DIR__')) exit;
$this->need('header.php');
?>

<style>
.friend-links {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 16px;
  padding: 0;
  margin: 1em 0;
  list-style: none;
}
.friend-links li {
  display: flex;
  align-items: center;
  padding: 10px;
  border-radius: 8px;
  background: #f8f8f8;
  transition: background 0.3s;
}
.friend-links li:hover {
  background: #f0f0f0;
}
.friend-links img {
  width: 32px;
  height: 32px;
  object-fit: cover;
  border-radius: 6px;
  margin-right: 10px;
  flex-shrink: 0;
}
.friend-links .no-icon {
  width: 32px;
  height: 32px;
  background: #ddd;
  border-radius: 6px;
  margin-right: 10px;
  flex-shrink: 0;
}
.friend-links a {
  color: #333;
  text-decoration: none;
  font-weight: 500;
}
</style>

<main class="links-page">
  <article class="post">
    <h1 class="post-title" itemprop="name headline">
            <a itemprop="url" href="<?php echo Helper::options()->siteUrl('friends.html'); ?>"><?php $this->title() ?></a>
        </h1>
    <div class="post-content">

<?php
$lines = explode("\n", trim($this->text));
$linkList = [];

foreach ($lines as $line) {
    $line = trim($line);
    if ($line === '') continue;

    // 如果包含 HTML 标签，则原样输出
    if (preg_match('/<[^>]+>/', $line)) {
        echo $line . "\n";
        continue;
    }

    // 默认处理为友链：名称,链接[,图标]
    $parts = explode(',', $line);
    if (count($parts) >= 2) {
        $name = htmlspecialchars(trim($parts[0]));
        $url = htmlspecialchars(trim($parts[1]));
        $icon = isset($parts[2]) ? htmlspecialchars(trim($parts[2])) : '';
        $linkList[] = ['name' => $name, 'url' => $url, 'icon' => $icon];
    }
}

// 输出友链
if (!empty($linkList)) {
    echo '<ul class="friend-links">';
    foreach ($linkList as $link) {
        echo '<li>';
        if ($link['icon']) {
            echo '<img src="' . $link['icon'] . '" alt="' . $link['name'] . ' icon" loading="lazy">';
        } else {
            echo '<div class="no-icon"></div>';
        }
        echo '<a href="' . $link['url'] . '" target="_blank" rel="noopener noreferrer">' . $link['name'] . '</a>';
        echo '</li>';
    }
    echo '</ul>';
}
?>

    </div>
  </article>
</main>

<?php $this->need('comments.php'); ?>
<?php $this->need('footer.php'); ?>
