で囲む
text = text.replace(/```([\s\S]+?)```/g, (match, code) => {
const key = `\u2063{{PLACEHOLDER${placeholderIndex++}}}\u2063`;
placeholders[key] = `${code.replace(/^\s*\n/, '')}
`;
return key;
});
// コードブロックの退避
text = text.replace(/`([^`\n]+)`/g, (_, code) => {
const key = `\u2063{{PLACEHOLDER${placeholderIndex++}}}\u2063`;
placeholders[key] = `${code}`;
return key;
});
// コロンで囲まれた絵文字をプレースホルダーに退避
text = text.replace(/:([a-zA-Z0-9_]+):/g, (match) => {
const key = `\u2063{{PLACEHOLDER${placeholderIndex++}}}\u2063`;
placeholders[key] = match; // 元の文字列を保存
return key;
});
// ユーザーIDをプレースホルダーに退避
text = text.replace(/@([a-zA-Z0-9_]+)/g, (match) => {
const key = `\u2063{{PLACEHOLDER${placeholderIndex++}}}\u2063`;
placeholders[key] = match; // 元の文字列を保存
return key;
});
// 独自構文などの装飾
text = text.replace(/\[\[buruburu (.+?)\]\]/g, '$1');
text = text.replace(/\[\[time (\d+)\]\]/g, (_, ts) => {
const d = new Date(parseInt(ts, 10) * 1000);
return `${d.toLocaleString()}`;
});
// マークダウン風装飾
text = text
.replace(/\*\*\*(.+?)\*\*\*/g, '$1')
.replace(/___(.+?)___/g, '$1')
.replace(/\*\*(.+?)\*\*/g, '$1')
.replace(/__(.+?)__/g, '$1')
.replace(/\*(.+?)\*/g, '$1')
.replace(/_(.+?)_/g, '$1')
.replace(/~~(.+?)~~/g, '$1')
.replace(/^>>> ?(.*)$/gm, '$1') // ここを修正
.replace(/\|\|(.+?)\|\|/g, '$1')
.replace(/^# (.+)/gm, '$1
')
.replace(/^## (.+)/gm, '$1
')
.replace(/^### (.+)/gm, '$1
')
.replace(/^- (.+)/gm, '・ $1');
// 行ごとに タグで囲む
const lines = text.split('\n').map(line => {
line = line.trim();
return line === '' ? '
' : `
${line}
`;
});
// プレースホルダーを戻す
let final = lines.join('');
for (const key in placeholders) {
const escapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
final = final.replace(new RegExp(escapedKey, 'g'), placeholders[key]);
}
return final;
}
function YouTube_and_nicovideo_Links(postText) {
const urlPattern = /(https:\/\/[^\s<>\[\]'"“”]+)/g;
const urls = postText.match(urlPattern);
let embedCode = '';
if (!urls) return null;
let embeddedOnce = false; // ← 埋め込みが1回されたかどうか
urls.forEach(url => {
if (embeddedOnce) return; // ← すでに埋め込みしたらスキップ
try {
const parsed = new URL(url);
const host = parsed.hostname.replace(/^www\./, '');
let videoId = '';
let videoTime = '0';
let iframe = false;
if (['youtube.com', 'youtu.be', 'm.youtube.com'].includes(host)) {
if (parsed.hostname === 'youtu.be') {
videoId = parsed.pathname.replace('/', '');
iframe = true;
} else if (parsed.searchParams.has('v')) {
videoId = parsed.searchParams.get('v');
iframe = true;
} else if (parsed.pathname.startsWith('/shorts/')) {
videoId = parsed.pathname.replace('/shorts/', '');
iframe = true;
}
if (parsed.searchParams.has('t') || parsed.searchParams.has('start')) {
videoTime = parsed.searchParams.get('t') || parsed.searchParams.get('start') || '0';
if (isNaN(parseInt(videoTime))) videoTime = '0';
}
if (iframe && videoId) {
embedCode = ``;
embeddedOnce = true;
}
} else if (['nicovideo.jp', 'nico.ms'].includes(host)) {
if (parsed.pathname.includes('/watch/')) {
videoId = parsed.pathname.split('/watch/')[1];
iframe = true;
} else {
videoId = parsed.pathname.replace('/', '');
iframe = true;
}
if (parsed.searchParams.has('from')) {
videoTime = parsed.searchParams.get('from');
if (isNaN(parseInt(videoTime))) videoTime = '0';
}
if (iframe && videoId) {
embedCode = ``;
embeddedOnce = true;
}
}else{
embedCode = null
}
} catch (e) {
// 無視
}
});
return embedCode;
}
function formatSmartDate(datetimeStr) {
const date = new Date(datetimeStr.replace(" ", "T"));
const now = new Date();
const diffMs = now - date;
const diffAbs = Math.abs(diffMs);
const future = diffMs < 0;
const pad = (n) => n.toString().padStart(2, '0');
const hhmm = `${pad(date.getHours())}:${pad(date.getMinutes())}`;
const y = date.getFullYear();
const m = date.getMonth();
const d = date.getDate();
const nowY = now.getFullYear();
const nowM = now.getMonth();
const nowD = now.getDate();
const dayDiff = Math.floor((new Date(y, m, d) - new Date(nowY, nowM, nowD)) / (1000 * 60 * 60 * 24));
if (!future && diffAbs < 30 * 1000) return "今";
if (future && diffAbs < 60 * 1000) return "まもなく";
if (future && diffAbs < 60 * 60 * 1000) return `${Math.floor(diffAbs / 1000 / 60)}分後`;
if (dayDiff === 0) return `今日 ${hhmm}`;
if (dayDiff === 1) return `明日 ${hhmm}`;
if (!future && y === nowY && m === 0 && d === 1) return `元日 ${hhmm}`;
if (y === nowY) return `${pad(m + 1)}/${pad(d)} ${hhmm}`;
return `${y}/${pad(m + 1)}/${pad(d)} ${hhmm}`;
}
async function createUeuseHtml(ueuse, selectedUniqid = null) {
let html = "";
let check = "";
var reuse = "";
let contentHtml = "";
var uniqid = "";
var userid = "";
var username = "";
var iconurl = "";
var datetime = "";
var favoritecount = 0;
var replycount = 0;
var reusecount = 0;
var is_favorite = false;
var is_bookmark = false;
var is_nsfw = false;
var abi = "";
var abi_date = "";
var abi_html = "";
var addabi = "";
var inyo = "";
var img1 = "";
var img2 = "";
var img3 = "";
var img4 = "";
var vid1 = "";
var img_html = "";
var vid_html = "";
var nsfw_html = "";
var nsfw_start_html = "";
var nsfw_end_html = "";
if (ueuse["userdata"]["role"].includes("official")) {
check = `
`;
}
if(ueuse["type"] == "Reuse"){
if(ueuse["ueuse"].length > 0){
reuse = ``;
if(!(ueuse["reuse"] == null)){
// カスタム絵文字を非同期に差し替え
var inyoreuseHtml = formatMarkdown(a_link(ueuse["reuse"]["ueuse"]));
inyoreuseHtml = await replaceMentions(inyoreuseHtml);
inyoreuseHtml = await replaceCustomEmojis(inyoreuseHtml);
inyo = `
`+inyoreuseHtml+`
`;
}else{
inyo = `
リユーズ元のユーズは削除されました。
`;
}
contentHtml = formatMarkdown(a_link(ueuse["ueuse"]));
uniqid = ueuse["uniqid"];
userid = ueuse["userdata"]["userid"];
username = ueuse["userdata"]["username"];
iconurl = ueuse["userdata"]["iconurl"];
datetime = ueuse["datetime"];
favoritecount = ueuse["favoritecount"];
replycount = ueuse["replycount"];
reusecount = ueuse["reusecount"];
is_favorite = ueuse["is_favorite"];
is_bookmark = ueuse["is_bookmark"];
is_nsfw = ueuse["nsfw"];
img1 = ueuse["photo1"];
img2 = ueuse["photo2"];
img3 = ueuse["photo3"];
img4 = ueuse["photo4"];
vid1 = ueuse["video1"];
abi = ueuse["abi"]["abi_text"];
abi_date = ueuse["abi"]["abi_date"];
}else{
if(!(ueuse["reuse"] == null)){
reuse = ``;
inyo = ``;
contentHtml = formatMarkdown(a_link(ueuse["reuse"]["ueuse"]));
uniqid = ueuse["reuse"]["uniqid"];
userid = ueuse["reuse"]["userdata"]["userid"];
username = ueuse["reuse"]["userdata"]["username"];
iconurl = ueuse["reuse"]["userdata"]["iconurl"];
datetime = ueuse["reuse"]["datetime"];
favoritecount = ueuse["reuse"]["favoritecount"];
replycount = ueuse["reuse"]["replycount"];
reusecount = ueuse["reuse"]["reusecount"];
is_favorite = ueuse["reuse"]["is_favorite"];
is_bookmark = ueuse["reuse"]["is_bookmark"];
is_nsfw = ueuse["reuse"]["nsfw"];
img1 = ueuse["reuse"]["photo1"];
img2 = ueuse["reuse"]["photo2"];
img3 = ueuse["reuse"]["photo3"];
img4 = ueuse["reuse"]["photo4"];
vid1 = ueuse["reuse"]["video1"];
abi = ueuse["reuse"]["abi"]["abi_text"];
abi_date = ueuse["reuse"]["abi"]["abi_date"];
}else{
reuse = ``;
inyo = ``;
contentHtml = "リユーズ元のユーズは削除されました。";
uniqid = ueuse["uniqid"];
userid = ueuse["userdata"]["userid"];
username = ueuse["userdata"]["username"];
iconurl = ueuse["userdata"]["iconurl"];
datetime = ueuse["datetime"];
favoritecount = ueuse["favoritecount"];
replycount = ueuse["replycount"];
reusecount = ueuse["reusecount"];
is_favorite = ueuse["is_favorite"];
is_bookmark = ueuse["is_bookmark"];
is_nsfw = ueuse["nsfw"];
img1 = ueuse["photo1"];
img2 = ueuse["photo2"];
img3 = ueuse["photo3"];
img4 = ueuse["photo4"];
vid1 = ueuse["video1"];
abi = ueuse["abi"]["abi_text"];
abi_date = ueuse["abi"]["abi_date"];
}
}
}else if(ueuse["type"] == "Reply"){
if(selectedUniqid != null && selectedUniqid == ueuse["uniqid"]){
reuse = `一番上のユーズに返信
`;
}else{
reuse = `一番上のユーズに返信
`;
}
inyo = ``;
contentHtml = formatMarkdown(a_link(ueuse["ueuse"]));
uniqid = ueuse["uniqid"];
userid = ueuse["userdata"]["userid"];
username = ueuse["userdata"]["username"];
iconurl = ueuse["userdata"]["iconurl"];
datetime = ueuse["datetime"];
favoritecount = ueuse["favoritecount"];
replycount = ueuse["replycount"];
reusecount = ueuse["reusecount"];
is_favorite = ueuse["is_favorite"];
is_bookmark = ueuse["is_bookmark"];
is_nsfw = ueuse["nsfw"];
img1 = ueuse["photo1"];
img2 = ueuse["photo2"];
img3 = ueuse["photo3"];
img4 = ueuse["photo4"];
vid1 = ueuse["video1"];
abi = ueuse["abi"]["abi_text"];
abi_date = ueuse["abi"]["abi_date"];
}else if(ueuse["type"] == "User"){
html = `
`;
return html;
}else{
reuse = ``;
inyo = ``;
contentHtml = formatMarkdown(a_link(ueuse["ueuse"]));
uniqid = ueuse["uniqid"];
userid = ueuse["userdata"]["userid"];
username = ueuse["userdata"]["username"];
iconurl = ueuse["userdata"]["iconurl"];
datetime = ueuse["datetime"];
favoritecount = ueuse["favoritecount"];
replycount = ueuse["replycount"];
reusecount = ueuse["reusecount"];
is_favorite = ueuse["is_favorite"];
is_bookmark = ueuse["is_bookmark"];
is_nsfw = ueuse["nsfw"];
img1 = ueuse["photo1"];
img2 = ueuse["photo2"];
img3 = ueuse["photo3"];
img4 = ueuse["photo4"];
vid1 = ueuse["video1"];
abi = ueuse["abi"]["abi_text"];
abi_date = ueuse["abi"]["abi_date"];
}
if(abi != "" && typeof abi === "string") {
abi = formatMarkdown(a_link(abi));
abi = await replaceMentions(abi);
abi = await replaceCustomEmojis(abi);
abi_html = `
`+await replaceCustomEmojis(username)+`さんが追記しました
`+abi+`
`+formatSmartDate(abi_date)+`
`;
addabi = ``;
}else{
abi_html = ``;
if(global_userid == userid){
addabi = ``;
}else{
addabi = ``;
}
}
let is_fav = {
"class": "favbtn",
"icon": "../img/sysimage/favorite_1.svg#favorite"
};
if (is_favorite === true) {
is_fav = {
"class": "favbtn favbtn_after",
"icon": "../img/sysimage/favorite_2.svg#favorite"
};
}
let is_reu = {
"class": "reuse"
};
if(ueuse["type"] == "Reuse"){
if(!(ueuse["ueuse"].length > 0)){
if (global_userid == ueuse["userdata"]["userid"]) {
is_reu = {
"class": "reuse reuse_after"
};
}
}
}
let is_bok = {
"class": "bookmark",
"icon": "../img/sysimage/bookmark_1.svg#bookmark_1"
};
if (is_bookmark === true) {
is_bok = {
"class": "bookmark bookmark_after",
"icon": "../img/sysimage/bookmark_1.svg#bookmark_1"
};
}
if(is_nsfw == true){
nsfw_html = `
NSFW指定がされている投稿です!
職場や公共の場での表示には適さない場合があります。
表示ボタンを押すと表示されます。
`
nsfw_start_html = ``
nsfw_end_html = ``
}
if(img1.length > 0){
if(img2.length > 0){
if(img3.length > 0){
if(img4.length > 0){
img_html = ``;
}else{
img_html = ``;
}
}else{
img_html = ``;
}
}else{
img_html = ``;
}
}else{
img_html = ``;
}
if(vid1.length > 0){
vid_html = `
`;
}
// カスタム絵文字を非同期に差し替え
contentHtml = await replaceMentions(contentHtml);
contentHtml = await replaceCustomEmojis(contentHtml);
if(ueuse["type"] == "Reuse"){
if(ueuse["ueuse"].length > 0){
if(YouTube_and_nicovideo_Links(ueuse["ueuse"])){
contentHtml = contentHtml+YouTube_and_nicovideo_Links(ueuse["ueuse"]);
}
}else{
if(YouTube_and_nicovideo_Links(ueuse["reuse"]["ueuse"])){
contentHtml = contentHtml+YouTube_and_nicovideo_Links(ueuse["reuse"]["ueuse"]);
}
}
}else{
if(YouTube_and_nicovideo_Links(ueuse["ueuse"])){
contentHtml = contentHtml+YouTube_and_nicovideo_Links(ueuse["ueuse"]);
}
}
html = `
`+reuse+`
`+nsfw_html+`
`+nsfw_start_html+`
`+contentHtml+`
`+img_html+`
`+vid_html+`
`+inyo+`
`+abi_html+`
`+nsfw_end_html+`
`+replycount+`
`+addabi+`
`;
return html;
}
function createAdsHtml(ads) {
if(!(ads == null || ads == "")){
var ads_html = ``;
return ads_html;
}else{
var ads_html = ``;
return ads_html;
}
}
// 投稿一覧を非同期で全部HTML化 → そのあと順番通りにappend
async function renderUeuses(ueuseData, selectedUniqid = null) {
if(ueuseData["success"] == false){
var errmsg;
if(ueuseData["error"] == "no_ueuse"){
errmsg = "ユーズがありません";
}else if(ueuseData["error"] == "bad_request"){
errmsg = "不正なリクエストが検出されました";
}
$("#postContainer").append(``+errmsg+`
`);
}else{
var htmlList = [];
var ueuseList = ueuseData["ueuses"];
for (const ueuse of ueuseList) {
const html = await createUeuseHtml(ueuse, selectedUniqid);
htmlList.push(html);
}
var ads = ueuseData["ads"];
const ads_html = createAdsHtml(ads);
htmlList.push(ads_html);
// 投稿順を保ったままDOMへ追加
for (const html of htmlList) {
$("#postContainer").append(html);
}
}
}