BiBTeX2Table
使用法
1. 「.bib」という名前のコードブロックにBibTeX形式のテキスト情報を記載
code:.bib
...
2. ページ左の「BibTeX」のロゴマークをクリックするとテーブルが出力される
https://gyazo.com/bb41c340656810e260d9e1dd0c4da541
code:script.js
scrapbox.PageMenu.addMenu({
title: 'NewButton',
onClick: () => bibtex2table(),
});
function bibtex2table() {
var codeblockURL = root + scrapbox.Project.name + '/' + scrapbox.Page.title + '/.bib';
console.log("Fetching data from URL:", codeblockURL); // ログを追加
fetch(codeblockURL)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.text();
})
.then(text => {
console.log("Data fetched successfully, formatting now."); // ログを追加
var formattedText = formatBibtex(text);
addFormattedTextToPage(formattedText);
})
.catch(error => {
console.error('There has been a problem with your fetch operation:', error);
});
}
function formatBibtex(bibtexText) {
var lines = bibtexText.split('\n');
var result = "table:bib\n"; // この行を追加して、resultを初期化
var key, value;
lines.forEach(line => {
line = line.trim();
if (line.startsWith('@')) {
result += " type\t+ line.match(/@(\w+)/)1 + "\n"; } else if (line.includes('=')) {
key, value = line.split('=').map(part => part.trim().replace(/,$/, '')); if (key === 'doi') {
} else if (value.startsWith('{') && value.endsWith('}')) {
value = value.slice(1, -1);
} else if (value.startsWith('"') && value.endsWith('"')) {
value = value.slice(1, -1);
}
result += ${key}\t${value}\n;
}
});
console.log("Formatted text:", result); // 処理された結果にログを追加
return result; // resultを返す
}
function addFormattedTextToPage(formattedText) {
console.log("Adding formatted text to the page:", formattedText);
if (scrapbox.Page.lines && scrapbox.Page.lines.length > 0) {
// 最初の行に追加する場合
const line = document.getElementById('L' + scrapbox.Page.lines0.id); const lastChar = line.querySelector('span.char-index:last-of-type');
const textarea = document.getElementById('text-input');
if (!lastChar || !textarea) {
console.error("Required elements not found in the page.");
return;
}
lastChar.dispatchEvent(new MouseEvent('click', {bubbles: true, cancelable: true}));
textarea.dispatchEvent(new KeyboardEvent('keydown', {bubbles: true, cancelable: true, keyCode: 35}));
textarea.value = formattedText;
textarea.dispatchEvent(new InputEvent('input', {bubbles: true, cancelable: true}));
console.log("Formatted text should now be added to the page.");
} else {
console.error("Cannot add text to the page: No lines found on the page.");
}
}